You need to initialize or "Seed" the random number generator.
Look into using the C function srand prior to using rand.
-----Original Message-----
From: midrange-l-bounces@xxxxxxxxxxxx [mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Emily Smith
Sent: Thursday, December 04, 2008 3:39 PM
To: midrange-l@xxxxxxxxxxxx
Subject: random number generator oddity
List,
We have a program that uses 'rand' to generate random numbers. I call this program requesting an eight byte number and receive a number 81239122. I sign off, and a coworker signs onto my workstation he also calls the program requesting an eight byte number, and also receives 81239122. He signs off, I sign back on, run the same program and receive a completely different number 73449833. I sign off. He signs back on again calls the program and receives 81239712.
So we've been testing this for about an hour using different scenarios (one of us signs in at the same workstation and calls the program then signs off and the other signs on, we sign in at the same time to different workstations and call the program simultaneously, one signs in at one workstation calls the program and then signs off the other signs on to a second workstation and calls the program etc.) multiple times. Sometimes, we would each get a random number that was the same as the other. Other times the numbers would be completely different. Still other times the first four or six digits would be the same but the last two or four would be the same.
Could anyone shed any light as to how 'rand' works and why it doesn't seem to truly generate a random number? Is there something wrong with the program that uses 'rand' to generate the random number?
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact
[javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.