After a while navigating throught questions and googling a bunch, I've found myself in the pain that none of the 'random' generators were REALLY random, what do I mean by random? total randomness, ALL algorithms I've found after compiling them and running multiple times printing tons of values the results were the same or at least a pattern was to be found. This is very bad, I'm doing a random number generator for a MMO server, which the function will be from upgrading items, into splitting a set of players into teams, patterns are always, always bad, imagine everytime I restart the program the same order of numbers being generated. My thoughts are that the best way to get a random number is to mess with time, you can never know what will be the exact time something will happen, and if the time is very precise even better. I came up with a solution that is great.
inline __int64 NanoTime()
{
struct { int low, high; } Nano;
__asm push EAX
__asm push EDX
__asm __emit 0fh __asm __emit 031h
__asm mov Nano.low, EAX
__asm mov Nano.high, EDX
__asm pop EDX
__asm pop EAX
return *(__int64 *)( &Nano );
}
static DWORD g_Prev = NanoTime();
__int64 xRandom( void )
{
__int64 Now = NanoTime() - g_Prev / 2;
CString strRand, strLast;
strRand.Format( "%I64d", Now );
strLast = strRand.GetAt( strRand.GetLength()-1 );
strLast += strRand.GetAt( strRand.GetLength()-2 );
return( atoi( strLast ) );
}
Althought the text is big, the question is simple I need a direction towards a real number generator which has the smallest algorithm as possible, my solution being called hundreds of thousands times each second is not what I'm looking for, it is only here to give a hint of the direction I need.
I have already looked into boost
, things like not using the lowest-bit order, time(0)
etc.. but all have a pattern.