Quality Test of a Pseudo Random Number Generator

Discussion of chess software programming and technical issues.

Moderators: hgm, Dann Corbit, Harvey Williamson

User avatar
smrf
Posts: 484
Joined: Mon Mar 13, 2006 11:08 am
Location: Klein-Gerau, Germany

Quality Test of a Pseudo Random Number Generator

Post by smrf »

Is someone able/willing to comment on the good/bad quality (entropy) of the following C++ written 32 Bit generator I wrote? Also a hint on where to find such a testing routine would be very helpful.

Random.h

Code: Select all

#ifndef RandomH
#define RandomH

// =====================================================
// (C) 2012 Reinhard Scharnagl, 80992 Munich, Germany
// -----------------------------------------------------
// 32 Bit Pseudo Random Number Generator
// -----------------------------------------------------
// Assumption: datatype "unsigned" is having 32 bits
// =====================================================

class Random {
private:
  // Seed (buffer for creating follow-up numbers)
  unsigned m_Seed;

public:
  // constructor (randomizing)
  Random(void);

  // reset generator to a new starting point
  void __fastcall setSeed(unsigned newSeed);

  // create a starting point randomized by time
  unsigned __fastcall randomize(void);

  // create a following-up random number
  unsigned __fastcall rand(void);

  // create a 0-1-bit balanced random Zobrist key
  unsigned __fastcall rand01(void);

  // create a random number below a given limit > 0
  unsigned __fastcall rangedRand(unsigned limit);
};

#endif /* #ifndef RandomH */
Random.cpp

Code: Select all

#include <limits.h>
#include <time.h>

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#include "Random.h"

// constructor &#40;randomizing&#41;
Random&#58;&#58;Random&#40;void&#41; &#123;
  // initially randomize by time
  randomize&#40;);
&#125;

// reset generator to a new starting point
void __fastcall Random&#58;&#58;setSeed&#40;unsigned newSeed&#41; &#123;
  // store new seed
  m_Seed = newSeed;
&#125;

// create a starting point randomized by time
unsigned __fastcall Random&#58;&#58;randomize&#40;void&#41; &#123;
  // combining time and status quo of the seed
  return m_Seed = rand&#40;) * &#40;unsigned&#41;time&#40;0L&#41;;
&#125;

// create a following-up random number
unsigned __fastcall Random&#58;&#58;rand&#40;void&#41; &#123;
  // create by two selected prime numbers and store
  return m_Seed = m_Seed * 87641 + 98731;
&#125;

// create a 0-1-bit balanced random Zobrist key
unsigned __fastcall Random&#58;&#58;rand01&#40;void&#41; &#123;
  // Zobrist key candidate and its copy
  unsigned answer, answScan;
  // bit down-counter
  unsigned bitCountDown;

  // select next qualified Zobrist number
  do &#123;
    // create new candidate
    answScan = answer = rand&#40;);
    // load intended bit count into down-counter
    bitCountDown = 2 * CHAR_BIT;

    // analyze 1-bits of current candidate
    while &#40;answScan&#41; &#123;
      // register lowest 1-bit
      --bitCountDown;
      // clear lowest 1-bit
      answScan &= answScan - 1;
    &#125;

    // while unbalanced
  &#125; while &#40;bitCountDown&#41;;

  // found qualified Zobrist key
  return answer;
&#125;

// create a random number below a given limit > 0
unsigned __fastcall Random&#58;&#58;rangedRand&#40;unsigned limit&#41; &#123;
  // set a top limit for securing equal distribution
  unsigned topLimit = UINT_MAX - UINT_MAX % limit;
  // number candidate
  unsigned value;

  // create next random number below that top limit
  do &#123;
    value = rand&#40;);
  &#125; while &#40;value >= topLimit&#41;;

  // apply modulo to result
  return value % limit;
&#125;
rbarreira
Posts: 900
Joined: Tue Apr 27, 2010 3:48 pm

Re: Quality Test of a Pseudo Random Number Generator

Post by rbarreira »

http://www.phy.duke.edu/~rgb/General/dieharder.php
Dieharder is a random number generator (rng) testing suite.
User avatar
Kirill Kryukov
Posts: 492
Joined: Sun Mar 19, 2006 4:12 am

Re: Quality Test of a Pseudo Random Number Generator

Post by Kirill Kryukov »

smrf wrote:Is someone able/willing to comment on the good/bad quality (entropy) of the following C++ written 32 Bit generator I wrote? Also a hint on where to find such a testing routine would be very helpful.
You can check TestU01 - it's the state of the art battery of RNG tests. It also contains a number of good RNGs.
User avatar
smrf
Posts: 484
Joined: Mon Mar 13, 2006 11:08 am
Location: Klein-Gerau, Germany

Re: Quality Test of a Pseudo Random Number Generator

Post by smrf »

Beside the fact, that I have detected some weaknesses already myself, DieHard.exe in Windows crashes for me ....