any good methods for generating psuedo random numbers 
Author Message
 any good methods for generating psuedo random numbers

The following is a transcription in C from
"Practical algorithms in C++" by Bryan Flamig:

/* Minimal Standard Generator */
static uint32 seed;

static uint32 random_next() {
 static sint32 a=16807;
 static sint32 m=2147483647L;
 static sint32 q;
 static sint32 r;
 q=m/a;
 r=m%a;
 seed=a*(seed-(seed/q)*q)-(seed/q)*r;
 if(seed<0) seed+=m;
 return seed;

Quote:
}

--

Altera Ltd.                       http://www.*-*-*.com/
Quote:
>>TinyScheme download site:       http://www.*-*-*.com/

*** Reality is what refuses to disappear when you stop believing
*** in it (VALIS, Philip K.{*filter*})
--



Sun, 02 Dec 2001 03:00:00 GMT  
 any good methods for generating psuedo random numbers

Quote:

> The following is a transcription in C from
> "Practical algorithms in C++" by Bryan Flamig:

My favourite for both elegance and period is from Bruce Schneier's
_Applied Cryptography (2nd Ed.)_

static long s1 = 1;  /* A "long" must be 32 bits long */
static long s2 = 1;

#define MODMULT(a,b,c,m,s) q = s/a; s = b*(s-a*q) - c*q; if (s<0) s+=m

/* MODMULT(a,b,c,m,s) computes s*b mod m, provided that m=a*b+c and
0<=c<m */

/*
 * combinedLCG returns a pseudorandom real value in the range
 * (0,1).  It combines linear congruential generators with
 * periods of 2^31-85 and 2^31-249, and has a period that is
 * the product of these two prime numbers
 */

double combinedLCG (void)
{
  long q ;
  long z ;

  MODMULT (53668, 40014, 12211, 2147483563L, s1);
  MODMULT (52774, 40692,  3791, 2147483399L, s2);

  z = s1 - s2 ;

  if (z < 1 )
    z += 2147483562 ;
  return z * 4.656613e-10 ;

Quote:
}

void initLCG ( long InitS1, long InitS2 )
{
  s1 = InitS1 ;
  s2 = InitS2 ;

Quote:
}

"This generator works as long as the machine can represent all integers
between -2^31+85 and 2^31-85.  The variables s1 and s2 are global; they
hold the current state of the generator.  Before the first call they
must be initialised.  The variable s1 needs an initial value between
2147483562; the variable s2 needs an initial value between 1 and
2147483398.  The generator has a period somewhere in the neighborhood of
10^18."

--
The above may or may not represent my own views.  It quite probably
does not represent the views of HP.


--



Tue, 04 Dec 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Any good methods for generating pseudo random integer?

2. Psuedo Random Numbers

3. Generating random numbers in c++

4. Help: Want to generate N random numbers which all add up X.

5. Generating random numbers between 0 and x

6. Generating random numbers

7. Generating random numbers...

8. generate random number?

9. generating random numbers

10. DON'T KNOW HOW TO GENERATE RANDOM NUMBERS

11. How do I generate random numbers?

12. How to generate random integer number??

 

 
Powered by phpBB® Forum Software