# Thread: drand48(): generating pseudo random numbers in a range

1. ## drand48(): generating pseudo random numbers in a range

Hi!

I know you are going to tell me to search in the faq. But my problem is not there.

The thing is this: I diveded the space between -2 and 2, both in x and in y axes, in little cells of 0.04x0.04. I have to generate random numbers in each cell, in that range, but I have not found the way to manipulate drand48() function to do that.

¿Some idea?

Thanks a lot!"!

2. A search of the Web reveals that:
The drand48() and erand48() functions return non-negative, double-precision, floating-point values, uniformly distributed over the interval [0.0 , 1.0].

I note that 0.5 * 4 = 2 and -0.5 * 4 = -2. As such, you probably can get away with (drand48() - 0.5) * 4.0 to satisfy your range.

EDIT:
Oh wait, "in little cells of 0.04x0.04". Basically, you have 10000 cells? In that case it seems easier to use something like rand() to map out 10000 cells instead of risking floating point inaccuracy when rounding.

3. I found the way!!!

I need numbers between 0 and 0.04; next, between 0.04 and 0.08, and so on.

The algorithm should look like this:

r=drand48()/25+i, where i is 0, 0.04, 0.08, and so on.

Thanks anyway!