I am having trouble trying to generate a random unsigned __int64 value, does anyone have a quick and effective way of doing something like this? below is what i am doing, check the code below.
unsigned __int64 m_RandomKey = 0;
while(m_RandomKey == 0)
{
m_RandomKey = (unsigned __int64) rand() << 32 | rand();
}
What is the best way to generate a unsigned __int64 key so that is is hard to get the same key again after a while or even at all? it doesn't have to be unique as long as there remains a 1 in 18,446,744,073,709,551,615 chance of it not doing it again!
If you are using C++11, you can use std::mt19937_64
, a native 64 bits implementation of the Mersenne twister algorithm.
See http://en.cppreference.com/w/cpp/numeric/random.
It is available in Visual C++ 2010 and 2012 (http://msdn.microsoft.com/en-us/library/ee462314(v=vs.100).aspx).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With