Questions about Random Numbers
Hi all,
Just wondering if the rand() function in C/C++ is capable of ever producing a random set of numbers that look maybe like:
3,4,2,6,6,6,6,6,3,6,7,9,3,8,22,9
the point is can the rand() function produce numbers that repeat like how the 6 repeated 5 times, can it ever do that depending on the seed.
How many seeds in total can be given to the srand() function?
and finally, from a set of random numbers can the original seed be derived..eg.
I give an imaginary number set:
5,67,5,78,32,6,8,5,32,6,8,43,76,33,67
is there a way that I can derive at the seed that can give this random number sequence, so that next time I run the rand() function using that seed found, I get the exact number set again.
Any help is good help folks
Thanks...
Dark Star
---------------------------------------------You Only Live Once - Don't be afriad to take chances.
From the bit of testing I have done here, it is possible (not sure if it is O/S, Machine, etc specific though). With the following code, my ouput remains the same, no matter how many runs I give it.
The output is always:
From what I have seen the seed can be set anytime you want, however many times you want, but only the last set seed is used.
I'm not sure how you would go about determining the seed value of a given set of random numbers though. Would have to look at the code for generating random numbers for that.
If you do wish to get more random-like results, you should seed with the time:
Edited by - mr_dejao on March 14, 2001 10:01:51 AM
#include #include #include using namespace std;int main(int argc, char* argv[]){ // Seed the Random Number Generator srand(100); for (int i = 0; i < 10; i++) { cout << "#" << (i + 1) << ": '" << (rand() % 100) << "'\n"; } return 0;}
The output is always:
#1: '65'#2: '16'#3: '15'#4: '4'#5: '4'#6: '54'#7: '98'#8: '2'#9: '9'#10: '29'
From what I have seen the seed can be set anytime you want, however many times you want, but only the last set seed is used.
I'm not sure how you would go about determining the seed value of a given set of random numbers though. Would have to look at the code for generating random numbers for that.
If you do wish to get more random-like results, you should seed with the time:
srand((unsigned)time(NULL));
Edited by - mr_dejao on March 14, 2001 10:01:51 AM
The guy who invented the first wheel was an idiot. The guy who invented the other three, he was the genius.
In a high level language like C/C++ you do not write the program. You just write something that tells the compiler what to do. "rand()" and "srand()" are just series of characters that tell the compiler that the binary code should include a routine that generates random numbers and one or more calls to that routine. Exactly what code is actually generated depends on which compiler you use.
Random numbers can be anything. You could roll a thousand dices and get six every time. But I still do not know what code is generated by your compiler. A decent random number generator however should be able to generate any number and the probability should be the same for all numbers.
You have not told us what you need the random number generator for. If you need it for a game it may not make any difference what generator you use. But if you need it for encryption purposes and stuff like that you should write your own routine to make sure that the numbers actually ARE random.
That again depends on what code is generated by the compiler. This is exactly why you should not use rand() in encryption software.
Then I hope that this explanation will help you.
quote:
can the rand() function produce numbers that repeat like how the 6 repeated 5 times, can it ever do that depending on the seed
Random numbers can be anything. You could roll a thousand dices and get six every time. But I still do not know what code is generated by your compiler. A decent random number generator however should be able to generate any number and the probability should be the same for all numbers.
You have not told us what you need the random number generator for. If you need it for a game it may not make any difference what generator you use. But if you need it for encryption purposes and stuff like that you should write your own routine to make sure that the numbers actually ARE random.
quote:
and finally, from a set of random numbers can the original seed be derived..eg
That again depends on what code is generated by the compiler. This is exactly why you should not use rand() in encryption software.
quote:
Any help is good help folks
Then I hope that this explanation will help you.
![](wink.gif)
mr_dejao, the reason is obvious (and I''m sure you know it, but this is for any browser who doesn''t know): the random number generator will always put out the same numbers with the same seed, so with srand(100) you always seed with 100, so therefore, you get the same output...
--
WNDCLASSEX Reality;
...
...
Reality.lpfnWndProc=ComputerGames;
...
...
RegisterClassEx(&Reality);
Unable to register Reality...what''s wrong?
---------
Dan Upton
Lead Designer
WolfHeart Software
--
WNDCLASSEX Reality;
...
...
Reality.lpfnWndProc=ComputerGames;
...
...
RegisterClassEx(&Reality);
Unable to register Reality...what''s wrong?
---------
Dan Upton
Lead Designer
WolfHeart Software
WNDCLASSEX Reality;......Reality.lpfnWndProc=ComputerGames;......RegisterClassEx(&Reality);Unable to register Reality...what's wrong?---------Dan Uptonhttp://0to1.orghttp://www20.brinkster.com/draqza
Yup. The seed changes each time you call rand, but it changes in a deterministic sequence. By default, the seed is 1 when you startup.
If you give any seed, and call rand N times, and then give that same seed, and call rand another N times, the two sequences will always be the same.
rand gives a uniform random variable; that is, the probability distribution of the process from 0 to RAND_MAX is the same. It will not give a Gaussian (normal) random number.
You can seed any number from zero to 0xFFFFFFFF (max unsigned long). There are, then, potentially that many (4294967295) random sequences possible. The only way I know of to find the original seed given a sequence would be to test each seed against the observed sequence; that could take a very, very long time. Also, you never know if there are multiple sequences that will be the same up to the Nth element, i.e. one seed might give you:
8 1 39 28 49
and another:
8 1 39 28 15
If you only checked the first 4 numbers, you''d assume they were the same. You have to get a fairly long sequence in order to be absolutely certain (probably a way to calculate the length of that sequence such that the probability of error is under a given epsilon).
If you give any seed, and call rand N times, and then give that same seed, and call rand another N times, the two sequences will always be the same.
rand gives a uniform random variable; that is, the probability distribution of the process from 0 to RAND_MAX is the same. It will not give a Gaussian (normal) random number.
You can seed any number from zero to 0xFFFFFFFF (max unsigned long). There are, then, potentially that many (4294967295) random sequences possible. The only way I know of to find the original seed given a sequence would be to test each seed against the observed sequence; that could take a very, very long time. Also, you never know if there are multiple sequences that will be the same up to the Nth element, i.e. one seed might give you:
8 1 39 28 49
and another:
8 1 39 28 15
If you only checked the first 4 numbers, you''d assume they were the same. You have to get a fairly long sequence in order to be absolutely certain (probably a way to calculate the length of that sequence such that the probability of error is under a given epsilon).
If you are going to make an encryption program, you shouldn''t be using a random number generator at all anyway. The seed should be something non-deterministic (which random functions aren''t). Bit tough to get something non deterministic on a computer though.
The rand function in C/C++ is almost always a linear congruential generator.
By picking a and c carefully we can make rand return 2^32 different values. You can think of rand() as reading consecutive values from a large table, and srand() as jumping to different locations in that table. There are lots of other PRNG''s(psudo random number generators) available. The Mersenne Twister is probablly the coolest. It doesn''t repeat for 2^19937-1 iterations. Don''t use _ANY_ of them for _REAL_ crypto.
int state;srand(int s) {state = s;}rand() { state = state * a + c; return state;}
By picking a and c carefully we can make rand return 2^32 different values. You can think of rand() as reading consecutive values from a large table, and srand() as jumping to different locations in that table. There are lots of other PRNG''s(psudo random number generators) available. The Mersenne Twister is probablly the coolest. It doesn''t repeat for 2^19937-1 iterations. Don''t use _ANY_ of them for _REAL_ crypto.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement