Drand48() returning same random numbers each time

In summary: The program is supposed to generate random cartesian coordinates for N points in three dimensions (where N is a command-line input). When run, the code does what it is supposed to: return N points with random coordinates. However, if John runs the code again, he gets the same list of coordinates every time. If John compiles and runs the code again, the same thing happens. The important point is that rand doesn't really return random numbers - that's impossible with an algorithm, it just returns a long list of non-repeating numbers with no pattern. To generate a different list you call seed() with a different starting value.
  • #1
A/4
56
3
I'm getting some curious behavior when I call the pseudo-random number generator drand48(). The program is supposed to generate random cartesian coordinates for N points in three dimensions (where N is a command-line input). When run, the code does what it is supposed to: return N points with random coordinates.

However, if I run the code again, I get the SAME values for the coordinates! If I recompile the code and run it again, the same thing happens.

Here's the relevant part of the code:

Code:
numPoints = atoi(argv[1]);

 for (i = 0; i <numPoints; i++) {
   Xrand = (2.*0.314159265)*drand48();
   Yrand = (2.*0.314159265)*drand48();
   Zrand = (2.*0.314159265)*drand48();
   Radius = sqrt(Xmod*Xmod+Ymod*Ymod+Zmod*Zmod);
   printf("%.5f %.5f %.5f %.5f\n",Xmod,Ymod,Zmod,Radius);
 }

The output looks something like this (e.g. for 5 points):

Code:
-0.31416 -0.31354 -0.28800 0.52910
-0.20317 -0.08507 -0.25677 0.33830
-0.25617 -0.00803 0.01681 0.25684
-0.02863 -0.16765 0.20816 0.26880
0.27126 0.04276 0.03525 0.27687

each time I run the program (and for any number of points these are the same).

Can anyone offer some insight? I've tried to find on-line samples of drand48() usage, but to no avail.
 
Technology news on Phys.org
  • #2
That's what it's supposed to do - otherwise there would be no way of testing it.
If you want a different sequence you need to seed() the generator with a different start number each time.
 
  • #3
try srand(time(0)); at the top of the main function...
 
  • #4
It might be seed48() for the rand48() library.

The important point is that rand doesn't really return random numbers - that's impossible with an algorithm, it just returns a long list of non-repeating numbers with no pattern.
To generate a different list you call seed() with a different starting value.
 
  • #5
Dr Transport said:
try srand(time(0)); at the top of the main function...

A quick caution.

If you want to run your program (or maybe a function that does this part of your program) many times in rapid succession, don't use the time(0) function to seed the generator. It will be called to close together, and the numbers in the sequences from the different runs will not be independent. This can really mess up any statistics you might perform.

John
 

What is the purpose of using the Drand48() function?

The Drand48() function is used to generate pseudo-random numbers in scientific computing. It is commonly used in simulations, statistical analysis, and other applications where random numbers are needed.

Why does the Drand48() function return the same random numbers each time?

The Drand48() function is designed to return the same sequence of numbers each time it is called with the same seed value. This is because it uses a deterministic algorithm to generate pseudo-random numbers, meaning that the same input will always produce the same output.

How can I get different random numbers from the Drand48() function?

To get different random numbers from the Drand48() function, you can change the seed value used to initialize the generator. This can be done by calling the srand48() function with a different seed value before calling Drand48(). Alternatively, you can use a different random number generator that is not based on a deterministic algorithm.

Is the Drand48() function truly random?

No, the Drand48() function is not truly random. It is a pseudo-random number generator, meaning that the numbers it produces are not truly random but appear to be so. This is because the numbers are generated using a deterministic algorithm, rather than being based on a truly random event.

Are there any alternatives to using the Drand48() function for generating random numbers?

Yes, there are many alternatives to using the Drand48() function for generating random numbers. Some popular options include the rand() function in C, the random package in Python, and the java.util.Random class in Java. These alternatives may use different algorithms and have different features, so it is important to choose the one that best fits your specific needs.

Similar threads

  • Programming and Computer Science
Replies
1
Views
749
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
22
Views
3K
  • Programming and Computer Science
Replies
1
Views
926
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
6
Views
4K
  • Programming and Computer Science
Replies
9
Views
2K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
Back
Top