Article View: sci.crypt
Article #2966Re: Wanted: very fast random number generator
From: Carl Ellison
Date: Wed, 27 Jan 1993 19:20
Date: Wed, 27 Jan 1993 19:20
55 lines
2135 bytes
2135 bytes
In article <C1H7tG.7At.2@cs.cmu.edu> tgl+@cs.cmu.edu (Tom Lane) writes:
>What I need is a very very fast random number generator; it does NOT need to
>be cryptographically strong.
My favorite fast generator:
#define N_RANS (65)
#define PRIMERADIX (1073741789)
long int ran_arr[ N_RANS ] ;
int ran_rdr = 0 ;
/* --------------------------------------------------------------------- */
/* raw_ran -- compute the next raw random number. */
/* This routine uses only addition. */
/* --------------------------------------------------------------------- */
long int raw_ran()
{
int nrdr ;
long int v ;
nrdr = ran_rdr + 1 ;
if ( N_RANS <= nrdr ) nrdr = 0 ;
v = ran_arr[ ran_rdr ] + ran_arr[ nrdr ] ;
if ( v >= PRIMERADIX ) v -= PRIMERADIX ; /* poor man's MOD function */
ran_arr[ ran_rdr ] = v ;
ran_rdr = nrdr ;
return ( v ) ;
} /* end of raw_ran */
/* --------------------------------------------------------------------- */
/* ran_radix --- return a random integer, radix r. */
/* --------------------------------------------------------------------- */
long int ran_radix( r )
long int r ;
{
return ( raw_ran() % r ) ;
} /* end of ran_radix */
You should fill the ran_arr with true random numbers, but I sometimes
use a typed string and then ask for (N_RANS)**2 raw_ran() values
before using a real one.
--Carl
--
-- <<Disclaimer: All opinions expressed are my own, of course.>>
-- Carl Ellison cme@sw.stratus.com
-- Stratus Computer Inc. M3-2-BKW TEL: (508)460-2783
-- 55 Fairbanks Boulevard ; Marlborough MA 01752-1298 FAX: (508)624-7488
Technical Details
Message-ID:
<1k6n9tINNsik@transfer.stratus.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!ulowell!m2c!nic.umass.edu!noc.near.net!transfer.stratus.com!ellisun.sw.stratus.com!cmeReferences:
<C1H7tG.7At.2@cs.cmu.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!ulowell!m2c!nic.umass.edu!noc.near.net!transfer.stratus.com!ellisun.sw.stratus.com!cme From: cme@ellisun.sw.stratus.com (Carl Ellison) Newsgroups: sci.crypt Subject: Re: Wanted: very fast random number generator Message-ID: <1k6n9tINNsik@transfer.stratus.com> Date: 27 Jan 93 19:20:29 GMT References: <C1H7tG.7At.2@cs.cmu.edu> Organization: Stratus Computer, Software Engineering Lines: 55 NNTP-Posting-Host: ellisun.sw.stratus.com