Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > sci.physics > #608600

C/CPP's "srand( int Seed )" generates an array of (PseudoRandom) int's.

From Jeff-Relf.Me <@.>
Newsgroups comp.os.linux.advocacy, sci.physics
Subject C/CPP's "srand( int Seed )" generates an array of (PseudoRandom) int's.
Date 2016-12-10 23:09 -0800
Organization Glorb Internet Services, http://www.glorb.com
Message-ID <Jeff-Relf.Me@Dec.10--11.09P.Seattle.2016> (permalink)
References (1 earlier) <o2ho5i$n47$3@dont-email.me> <ahjgob040.ae@rooftop.invalid> <o2i132$ll7$1@dont-email.me> <aybudaoc993.ag@rooftop.invalid> <o2i755$3d9$1@dont-email.me>

Cross-posted to 2 groups.

Show all headers | View raw


You ( deplorable owl ) wrote:
> I want to know the best approach for randomizing
> over a huge range (far exceeding RAND_MAX) with minimal dups.

C/CPP's "srand( int Seed )" generates an array of (PseudoRandom) int's;
i.e. it's a "Linear Congruential Generator" (LCG).

The code looks like this:
//  Deal() returns a random int; 
//  the first call to Deal() shuffles "the deck" ( an array of int's ).

#include <Windows.H>
#include <StdIO.H>

__int64 _Tics ;
#define  Tics  ( QueryPerformanceCounter( ( LARGE_INTEGER * ) &_Tics ), _Tics )
#define LOOP  while ( 1 )
#define Loop( N )  int J = -1, LLL = N ; while ( ++ J < LLL )
#define LoopD( N )  int J = N ; while( -- J )

const int HighResQual = 0x400, DecM = HighResQual * 4- 1
, LongLeg = 63, Short_Leg = 37, BothLegs = LongLeg + Short_Leg
, Seed_Buf_Sz = 2 * BothLegs - 1, StopBit = 1<<30
, EvenBits = StopBit - 2, Rand_Mask = StopBit - 1 ;

int LegsBuf[ BothLegs ], HighResBuf[ HighResQual ];
pInt  RanE = HighResBuf + BothLegs, RanP ;
_LnP  Dec = _LnP( HighResBuf );

Shuffle () {
  int StreamSperation = 70 - 1, Seed_Buf[ Seed_Buf_Sz ]
  , Seed = Tics, Seed_2 = Seed + 2 & EvenBits;  RanP = RanE ;
  { Loop( BothLegs ) {
      Seed_Buf[ J ] = Seed_2, Seed_2 <<= 1 ;
      if ( Seed_2 >= StopBit )  Seed_2 -= EvenBits ;  } }

  memset( Seed_Buf + BothLegs, 0, ( BothLegs - 1 ) * szInt );
  Seed_Buf[ 1 ] ++, Seed_2 = Seed & Rand_Mask ;
  LOOP {  { LoopD( BothLegs )  Seed_Buf[ 2 * J ] = Seed_Buf[ J ];  }
    { pInt  P = Seed_Buf + 1, R = Seed_Buf + Seed_Buf_Sz - 1;
      Loop( BothLegs - LongLeg / 2 - 1 )
        P[ 2 * J ] = R[ -2 * J ] & EvenBits;  }
    { pInt  B = Seed_Buf, P = B + Seed_Buf_Sz, R = B + Short_Leg - 1 ; 
      LoopD( BothLegs ) {  if ( ! ( * -- P & 1 ) )  continue; 
      R[ J ] = R[ J ] - * P & Rand_Mask ; 
      B[ J - 1 ] = B[ J - 1 ] - * P & Rand_Mask ;  } }
    if ( Seed_2 & 1 ) {
      memmove( Seed_Buf + 1, Seed_Buf, BothLegs * szInt );
      * Seed_Buf = Seed_Buf[ BothLegs ];
      if ( Seed_Buf[ BothLegs ] & 1 ) 
        Seed_Buf [ Short_Leg ] 
        = Seed_Buf [ Short_Leg ]
          - Seed_Buf[ BothLegs ] & Rand_Mask ;  }

    if ( ! Seed_2 && ! -- StreamSperation )  break;
    Seed_2 >>= 1 ;  }

  memmove( LegsBuf, Seed_Buf + Short_Leg, LongLeg * szInt ); 
  memmove( LegsBuf + LongLeg, Seed_Buf, Short_Leg * szInt );  }

int Deal() {  if ( ! RanP ) Shuffle();
  if ( RanP && RanP < RanE ) return  * RanP ++ ;
  RanP = HighResBuf, memmove( HighResBuf, LegsBuf, sizeof LegsBuf );
  { Loop( HighResQual - BothLegs ) 
      HighResBuf [ J + BothLegs ] 
      = HighResBuf [ J ] - HighResBuf [ J + LongLeg ] & Rand_Mask ;  }
  pInt  P = HighResBuf + HighResQual - Short_Leg ; 
  { Loop( Short_Leg ) 
      LegsBuf [ J ] = P [ J - LongLeg ] - P [ J ] & Rand_Mask ;  }
  P = HighResBuf + HighResQual - LongLeg ; 
  Loop( LongLeg ) LegsBuf [ J + Short_Leg ] = P [ J ] - LegsBuf [ J ] & Rand_Mask ;  
  return  * RanP ++ ;  }

Back to sci.physics | Previous | NextNext in thread | Find similar | Unroll thread


Thread

C/CPP's "srand( int Seed )" generates an array of (PseudoRandom) int's. Jeff-Relf.Me <@.> - 2016-12-10 23:09 -0800
  Re: C/CPP's "srand( int Seed )" generates an array of (PseudoRandom) int's. deplorable owl <owl@rooftop.invalid> - 2016-12-11 07:23 +0000

csiph-web