On Fri, 29 Apr 2005 02:17:47 +0000 (UTC), Rouben Rostamian

<ro****@pc18.math.umbc.edu> wrote:

In article <IF*********************@news20.bellglobal.com>,

Marc Dansereau <bo*******@hotmail.com> wrote:

I wonder what is the most efficient way to generate random point on a line

defined by 2 double points (x0,y0) and (x1,y1).

Generate a random number t in the interval [0,1]. Then let:

x = (1-t)*x0 + t*x1;

y = (1-t)*y0 + t*y1;

I would write it as:

x = x0 + t * (x1 - x0);

y = y0 + t * (y1 - y0);

which only does one multiply per expression. It also reflects the way I

think about lines (translate the start of the line to be the origin,

scale and then translate back).

Incidentally, using the interval [0,1] assumes that the endpoints of

the line are inclusive. This may be a problem, since a lot of random

number generators are in the (usually more useful) range [0,1) which

would exclude the end of the line (I've fallen into that trap before).

I've had to resort to using two generator functions:

double randf(void)

{

return rand() / (double)(RAND_MAX + 1);

}

double randfinc(void)

{

return rand() / (double)RAND_MAX;

}

Similarly for (0,1) and (0,1] if needed (I've needed the former but not

the latter).

(Actually I use a more consistent but non-Standard RNG instead of

rand(), see other threads about RNGs...)

Chris C