setting a random seed

Started by
10 comments, last by Crispy 20 years, 5 months ago
srand() doesn''t seem to be working - each time I run the program, different results are produced by rand()... Why is that?
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Advertisement
I think you''ll need to clarify that a little. The way I read it, your rand() calls are giving you random numbers and that is bad. That seems contradictory.
srand() is the function that sets the seed. If you use srand(SOME_CONSTANT);, then calls to rand() will be the same each the the program is run. He is saying that this is not happening, which is really odd. Maybe showing more of the source code would help.
*each time the program is run
At certain points during program flow I need to create a sequence of random numbers that I want to have "general" control over (these are for an object generator and hundreds or even thousands of random numbers need to be generated that define the general look of the object). For that I want to set a constant seed before each of these sequences, but setting the seed using srand() doesn''t seem to be working (the sequence of random numbers generated after calling srand() with a constant argument changes each time I run the program - the constant being defined as srand(23343) before compiling).

In other words, you understood me correctly, but since so many not-so-but-still-random numbers are involved, using the built-in generator is much more convenient and actually makes. I know this is vague, but the topic involved is currently slightly sensitive.

* insert ''pseudo'' in front of each ''random''
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Ok so you want the same random numbers each time you run the program?

Use these:

34234434
57452899
24086542
34704502
51180985
84029670
30420724
13758923

I think these are pretty random, especially the last one. If you need more, email me.
quote:Original post by Anonymous Poster
srand() is the function that sets the seed. If you use srand(SOME_CONSTANT);, then calls to rand() will be the same each the the program is run. He is saying that this is not happening, which is really odd. Maybe showing more of the source code would help.


I''ll go through the code one more time - there''s still a possiblity I''m calling srand() in some other instances (which I don''t remember doing) with the time argument or something, which would make this by bad.

As for source code - there isn''t much to show (or, there''s about ~1 MB of it to show ) - it''s quite the usual:

srand(27635);
v = rand() % 100; <- produces a different value each time the program is run
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
If you make out a simple program that just uses srand and rand, does it work? If it does, then it''s something happening in your program, such as you suggested, calling srand () somewhere else.
quote:
srand(27635);
v = rand() % 100;

Are you using a literal like this? Or is just some example of the type of thing you''re doing? If you''re givin srand a variable, ensure that the values you''re passing are the same each time too.

Ro_Akira
quote:Original post by Ro_Akira
Are you using a literal like this? Or is just some example of the type of thing you''re doing? If you''re givin srand a variable, ensure that the values you''re passing are the same each time too.


It''s a part pseudocode example.

I''m not calling srand() anywhere else... Hmmm, this is weird - can it be called only once by a program instance?

"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Maybe it depends on the compiler? What one are you using?

I suggest writing a small test program - just a main method - in which you seed with 23343 or whatever, and then print out rand() 10 times or so. Then run this program a few times to see if the output is always the same. If it is not, somethings up. If it is, then theres some other problem youve overlooked in you real project.

This topic is closed to new replies.

Advertisement