Drand48() returning same random numbers each time

AI Thread Summary
The discussion revolves around the behavior of the pseudo-random number generator drand48() in generating random Cartesian coordinates for points in three dimensions. Users observed that running the program multiple times produced the same output, indicating that the random number sequence was not changing. The key solution offered is to seed the random number generator with a different value each time the program runs, which can be achieved by using srand(time(0)) at the beginning of the main function. This ensures that the sequence of random numbers varies with each execution. Additionally, a caution is provided regarding the use of time(0) for seeding if the program is run multiple times in quick succession, as this could lead to non-independent sequences that may affect statistical outcomes. The discussion emphasizes the importance of seeding for generating varied random sequences in programming.
A/4
Messages
56
Reaction score
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
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.
 
try srand(time(0)); at the top of the main function...
 
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.
 
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
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top