Jump to content
  • Advertisement
Sign in to follow this  
bentaberry

random not random enough for me - use sleep? and how?

This topic is 3053 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone, I am currently trying to code a binary search just to see how it works and ive not got to the actual stage of writing the binary search function but i did a test output of my sorted array and I realised that the random numbers are pretty rubbish. I am guessing this is because when i seed rand with the time and I create my array by using a "for" it creates the array so fast that you end up with lots of the same numbers. I figure I can fix this by adding sleep() inside the {} for the for statement but I cant figure out how to do it. What do I need to #include and how would i write the sleep()? I am using visual studio express 2008 just incase that changes anything. Kind Regards David

Share this post


Link to post
Share on other sites
Advertisement
Perhaps you could use time + loop counter as a seed? For the first iteration this would just use the current time, but even if the time is the same (I'm assuming millisecond precision?) in the next iteration the loop counter will have increased.

EDIT: By the way, do you have a good reason to re-seed on every loop iteration?

Share this post


Link to post
Share on other sites
Generally a function such as rand() should return a pseudo random sequence of numbers. This sequence is not dependent on time. I may be mistaken but the seed only serves as a factor in the sequence generation. Others on here can explain this in more detail.

I am not sure what function you are using, but the documentation should explain how it is generating the sequence.

Edit: Usually you will not seed the random function each time in a loop.

Share this post


Link to post
Share on other sites
Quote:
Original post by smc
Generally a function such as rand() should return a pseudo random sequence of numbers. This sequence is not dependent on time. I may be mistaken but the seed only serves as a factor in the sequence. Others on here can explain this in more detail.


Time is quite often used as seed because it's easy. By using the current time as seed you can avoid the catch-22 situation where you need a random number to seed your PRNG. Thus, if the time value happens to be the same in two separate cases, the pseudo-random sequences will be identical (assuming there are no other variable factors involved in the process).

Share this post


Link to post
Share on other sites
Seeding from the current time should not be an issue, unless you seed before every rand, which you shouldn't do. Seed once, at the beginning of your program and then just call rand whenever you need a new number. What does your loop look like?

Share this post


Link to post
Share on other sites
Quote:
Original post by Windryder
Quote:
Original post by smc
Generally a function such as rand() should return a pseudo random sequence of numbers. This sequence is not dependent on time. I may be mistaken but the seed only serves as a factor in the sequence. Others on here can explain this in more detail.


Time is quite often used as seed because it's easy. By using the current time as seed you can avoid the catch-22 situation where you need a random number to seed your PRNG. :)


What I mean is the function itself is not dependent on time. Using time as a seed value is simply a scalar... it is an arbitrary number used by the function for the purposes of sequence generation.

In other words if I call for the next number at T=1, the value returned will be the same as if I had called for it a T=100. The sequence is predetermined.

It is entirely possible that a particular implementation is dependent on some timer function in which case I am wrong.

Share this post


Link to post
Share on other sites
answer to the actual question:


#include <windows.h>
Sleep(TimeToSleepInMilliSeconds);


Sleep at msdn

And I have a question with may lead to the cause of the problem.
Do you set the seed every time you need a random value (or every time inside the {} for the for statement)?
If so:
You must only set the seed once (or at least not every time you need a random value). Because srand sets the seed for the random algorithm (I don't know for sure if it's called a algorithm) but also resets it. What I mean is this:

srand(10);
rand() % 100 // = 45 (for example).
rand() % 100 // = 89
rand() % 100 // 2

srand(10);
rand() % 100 // = 45
srand(10);
rand() % 100 // = 45
srand(10);
rand() % 100 // = 45




I can't make up from your message if you do that but if so that is probably the cause of the problem.

[Edited by - flammable on February 8, 2010 8:19:18 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by smc
[...]

In other words if I call for the next number at T=1, the value returned will be the same as if I had called for it a T=100. The sequence is predetermined.

It is entirely possible that a particular implementation is dependent on some timer function in which case I am wrong.


Yes, the sequence is determined the moment you call srand() or whatever function you use to seed your particular randomizer. All implementations I am aware of will produce the same sequence given the same seed. However, if I've understood the OP's problem correctly, it basically boils down to the fact that he re-seeds the randomizer repeatedly in a loop, but sometimes one loop iteration doesn't take long enough for the time value (which in the case of GetTickCount() has millisecond precision) to increase. This would, as you pointed out, result in the same sequence being generated twice.

Share this post


Link to post
Share on other sites
Thanks for responses guys I will look them over carefully to understand how the rand function works and see if it is the problem or not.

btw just to clear things up - I only seed rand once - i might have not worded that very well sorry. inside the for {} i have int var = rand();

Thanks for help again everyone.

Kind Regards
David

Share this post


Link to post
Share on other sites
Just to confirm guys by adding the sleep the randomness seems to be the same and just makes the program slower :)

I guess that just looking at the numbers in a sorted array skewed my way of thinking :)

Thanks again guys
David

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!