scheme shell

Thirty Two Bit Random Integers

Fast generator for pseudo-random 32-bit integers

This is the "fast" linear congruential pseudo-random number generator `ranqd1' from chapter 7 of Numerical Recipes. Note that because Scheme systems generally offer native support for "big" integers, one has to use `modulo' in the Scheme code to emulate the behavior of 32-bit Fortran and C systems.

See for details.

Note that this is a very old, and very very poor, pseudo-random number generator. You should not use the code described on this page for any serious purpose, such as a Monte-Carlo simulation. Under Linux /dev/urandom (see RandomIntegerFromDevURandom for details) is an excellent source for good random numbers. You might also want to check the random() library function of the standard C library.

This code is also available from


The Code

   (define (gen-ranqd1 seed)
     (let ((random seed)
           (2**32 #x100000000))
       (lambda ()
         (set! random (modulo (+ (* random 1664525) 1013904223) 2**32))


You can use the following code to test `ranqd1' with your Scheme implementation:

   (let ((random (gen-ranqd1 0))
         (expected-results '(#x3c6ef35f
     (for-each (lambda (expected)
                 (let ((rnd (random)))
                   (if (not (= rnd expected))
                       (error (format #f "FAILED: expected ~a, got ~a" expected rnd)))))

More on Random Numbers

SRFI-27 ( by Sebastian Egner specifies an interface to sources of random bits. There is also a workshop on a portable random number generator in Scheme ( by John David Stone.

ThirtyTwoBitRandomIntegers - raw wiki source | code snippets archive