-1

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.

Vinícius
  • 15,498
  • 3
  • 29
  • 53
  • I suggest you post in the cryptography section. The reality is that true randomness cannot be guaranteed, but to have some semblance of randomness, you will need to run the algorithm through statistical tests to ensure randomness. – Gustavo Litovsky Dec 18 '12 at 23:08
  • Random.org has [an API](http://www.random.org/clients/http/). – Joseph Mansfield Dec 18 '12 at 23:09
  • http://stackoverflow.com/questions/1912199/better-random-algorithm – adripanico Dec 18 '12 at 23:10
  • How about atmospheric noise? – Vultour Dec 18 '12 at 23:17
  • 4
    "Imagine everytime I restart the program the same order of numbers being generated" --> Ehm, are you sure you're seeding the random generator? For something like you're doing even the standard `rand` should actually be sufficient, though I'd always use the new functionality from `` – KillianDS Dec 18 '12 at 23:31
  • Have you calculated entropy of generated sequences? – hate-engine Dec 18 '12 at 23:57

1 Answers1

5

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.

Frankly, it sounds like the algorithms you've tried were pretty poor, or there was something sub-optimal about how you were using or assessing them.

If the standard rand() is not sufficient (and that's a big if!), your best bet is a higher-grade pseudo-random generator, such as Mersenne Twister. Such algorithms have been demonstrated to pass various tests for statistical randomness.

If you get to choose what hardware you use, recent (Ivy Bridge) Intel hardware offers an on-chip random number generator with a built-in entropy source: RDRAND. This offers good randomness and high throughput, but is probably an overkill for your needs.

imagine everytime I restart the program the same order of numbers being generated

If that's an issue, it sounds like you're not seeding your generator correctly. If you do it once per run, and use the current time as the seed, you will get different random sequences on different runs.

I came up with a solution that is great.

If I am reading your code correctly, your entropy source is TSC modulo 100. Try calling your generator in a tight loop and see how random it really is.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 3
    "were pretty poor, or there was something sub-optimal" or else the questioner has the common human talent of spotting apparent patterns in random data. – Steve Jessop Dec 18 '12 at 23:24