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


Groups > sci.physics > #608600 > unrolled thread

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

Started byJeff-Relf.Me <@.>
First post2016-12-10 23:09 -0800
Last post2016-12-11 07:23 +0000
Articles 2 — 2 participants

Back to article view | Back to sci.physics

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

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

FromJeff-Relf.Me <@.>
Date2016-12-10 23:09 -0800
SubjectC/CPP's "srand( int Seed )" generates an array of (PseudoRandom) int's.
Message-ID<Jeff-Relf.Me@Dec.10--11.09P.Seattle.2016>
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 ++ ;  }

[toc] | [next] | [standalone]


#608602

Fromdeplorable owl <owl@rooftop.invalid>
Date2016-12-11 07:23 +0000
Message-ID<ahb0a11.bu00b3@rooftop.invalid>
In reply to#608600
In comp.os.linux.advocacy Jeff-Relf.Me <@.> wrote:
> 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;


void srand(unsigned int seed);


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


Don't have Windows.H here.


> #include <StdIO.H>
> 

Don't have StdIO.H here either.

anon@lowtide:~/code/dfsdates$ cat foo.c
#include <StdIO.H>

int main(int argc, char *argv[])
{
  return 0;
}
anon@lowtide:~/code/dfsdates$ gcc -Wall -o foo foo.c
foo.c:1:19: fatal error: StdIO.H: No such file or directory
 #include <StdIO.H>
                   ^
compilation terminated.
anon@lowtide:~/code/dfsdates$ 


> __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 ++ ;  }


Nice ascii art.

[toc] | [prev] | [standalone]


Back to top | Article view | sci.physics


csiph-web