+ 2

Extremely Random

Based on what I've seen in the C++ tutorial, it seems that if some part of my code (say, a loop) has to be executed several times a second (or at the same time on different devices), it will produce the same result even if srand(time(NULL)) is included, because the UNIX timestamp has a precision of one second. So... What would be an effective way to take care of this issue? I was guessing something like srand(time(NULL) + rand()) would sorta work, what do you think?

21st May 2018, 8:05 PM
Zuke
Zuke - avatar
12 Respostas
+ 1
Not necessarily, here is the function that rand uses: Maybe not exactly this, but this is how I implemented it, it generates the same sequence as c++'s rand function. namespace custom { unsigned seed = 1u; void srand(unsigned seed_) { seed = seed_; } int rand() { seed = (214013 * seed + 2531011) % 2147483648; return seed / 65536; } } As you see, rand calculates a value using the old seed and assigns it to seed again, to be used the next time rand is called. The seed is then divided by 65536 and that is returned. So I guess you can get the seed by multiplying rand() by 65536 yea, but only if the original seed was divisible by 65536 in the first place.
21st May 2018, 9:07 PM
Dennis
Dennis - avatar
+ 4
rand and srand are superseded by the (C++11) <random> header. check out std::random_device, std::mt19937 and std::uniform_int_distribution. Here's a basic example: http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution
22nd May 2018, 12:16 AM
Schindlabua
Schindlabua - avatar
+ 3
Nope, rand() changes the seed, so when you call rand() again it returns a different number. Only when you srand it again with the same previous seed will rand return the same number. rand in c++ is a linear congruential generator, check out https://en.wikipedia.org/wiki/Linear_congruential_generator to learn more about it :)
21st May 2018, 8:37 PM
Dennis
Dennis - avatar
+ 2
Zuke, std::random_device does exactly that (if your system supports it). It's not "direct" but your operating system will supply you with a source of randomness by reading all sorts of noise levels. So, use the example I posted above for good quality random numbers :)
23rd May 2018, 4:48 PM
Schindlabua
Schindlabua - avatar
+ 2
srand(time(NULL)) srand(rand()+rand()%rand()) cout<<rand()<<endl;
20th Jun 2018, 3:32 PM
Shahil Ahmed
Shahil Ahmed - avatar
+ 1
Why not just place srand(time(0)) as the first line in the main function? There is no need to seed it multiple times. Or even better, use the random functions from the <random> header: http://www.cplusplus.com/reference/random/
21st May 2018, 8:31 PM
Dennis
Dennis - avatar
+ 1
Alright, thanks!
21st May 2018, 8:37 PM
Zuke
Zuke - avatar
+ 1
you could access your hardware sensors and get truly random seeds to generate your random values. for example the noise level from the microphone or the color of random pixels in a pic from the webcam or the serial number of the drive (assigned by manufacturer and or OS). the list goes on and you could even combine more options to increase the seed randomness and consequently the random value.
22nd May 2018, 4:28 PM
seamiki
seamiki - avatar
+ 1
@seamiki Oh, that is interesting! Is there a direct way to access those through C++?
23rd May 2018, 4:18 PM
Zuke
Zuke - avatar
0
But doesn't that mean rand() would give out the same number for a loop executed several times a second?
21st May 2018, 8:32 PM
Zuke
Zuke - avatar
0
So, you mean the new seed will be equivalent to the what rand() returns?
21st May 2018, 8:51 PM
Zuke
Zuke - avatar
0
@Jay Williams What I want to do is to get as much different numbers for checking this solution as possible. https://code.sololearn.com/cnj1EJmDDqhg/?ref=app
23rd May 2018, 4:24 PM
Zuke
Zuke - avatar