random not random enough for me - use sleep? and how?
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
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?
EDIT: By the way, do you have a good reason to re-seed on every loop iteration?
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.
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.
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).
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?
Quote:Original post by WindryderQuote: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.
answer to the actual question:
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:
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]
#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 // = 89rand() % 100 // 2srand(10);rand() % 100 // = 45srand(10);rand() % 100 // = 45srand(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]
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.
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement