Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a random bit64 value

Tags:

c++

visual-c++

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!

like image 683
pick4pony Avatar asked Mar 24 '23 01:03

pick4pony


1 Answers

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).

like image 71
SirDarius Avatar answered Mar 27 '23 13:03

SirDarius