+ 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?
12 Antworten
+ 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.
+ 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
+ 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 :)
+ 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 :)
+ 2
srand(time(NULL))
srand(rand()+rand()%rand())
cout<<rand()<<endl;
+ 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/
+ 1
Alright, thanks!
+ 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.
+ 1
@seamiki Oh, that is interesting! Is there a direct way to access those through C++?
0
But doesn't that mean rand() would give out the same number for a loop executed several times a second?
0
So, you mean the new seed will be equivalent to the what rand() returns?
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