Re: Simple Random Number Generator/Function

From: Hugo Pfoertner (nothing_at_abouthugo.de)
Date: 03/08/05


Date: Tue, 08 Mar 2005 22:48:31 +0100

indras@efn.org schrieb:
>
> C, Pascal, or any languages that are easy to read will be fine. This
> generator will be used in DVD Authoring. There is a rand function in
> DVD specification, but few DVD players do not like them, that's why I
> would like to create one.

The Standard C generator from
Brian W Kernighan and Dennis M. Ritchie, The C Programming Language
(Second Edition) Prentice Hall Software Series, 1988

uses x(1)=1, x(n)=(1103515245 * x(n-1) + 12345) mod 2^31
a(n)=floor(x(n)/2^16)

C Program: static unsigned int next = 1; int rand( ) { next = next *
1103515245 + 12345; return ((next >>16) & 32767); }

If you want different behavior for different runs, you have to replace
"next=1" by something taking a variable input (e.g. clock). C provides
the function srand ( ... ); for this pupose.

Hugo



Relevant Pages