Archived

This topic is now archived and is closed to further replies.

Crispy

setting a random seed

Recommended Posts

Guest 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.

Share this post


Link to post
Share on other sites
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''

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
For this project I''m using VS .NET 2002 and I''ve landed on that srand() only has effect when called once per program instance. That is: if you were to call srand(time(NULL)) at the top of your program, any further calls to srand(MY_CONSTANT) will not generate a recurring output by rand(). Naturally, that''s a damn shame... The alternative, of course, is to use a custom-written random number generator, which I''m not prepared to do.

Share this post


Link to post
Share on other sites
it works correctly on my compiler (BCC55)...

you could try the "mersenne twister" (google for it) if you don''t want to work out your own random number algorithm.

Share this post


Link to post
Share on other sites