🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.functional
Article #983

Re: Random Number Generating Algorithm/script

#983
From: USENET News System
Date: Thu, 23 May 1991 00:56
52 lines
2701 bytes
In article <1991May20.161445@axion.bt.co.uk> dwestlan@axion.bt.co.uk (Dave Westland) writes:
>
>   Ladies and Gentlepersons,
>
>	Does anyone know any good random number generating algorithms, (or
>even not so good ones).

Here is mine, in Haskell.  I just touched it up a bit (and found a few
bugs with the seed initialisation), and I hope that it is suitable for
your application.  Hopefully someone else will find it useful too (I
wrote it for a Haskell Noughts and Crosses game).

Note: if you are not using arbitrary precision integers, maxInt must
be at least 2^46-1 (the quoted article describes a number of ways
around this).
_______________________________________________________________________________

E.Ireland@massey.ac.nz          Evan Ireland, School of Information Sciences,
  +64 63 69099 x8541          Massey University, Palmerston North, New Zealand.
_______________________________________________________________________________

module Random (randoms, random) where

-- Multiplicative linear congruential random number generator adapted
-- from "Random number generators: good ones are hard to find",
-- Stephen K. Park and Keith W. Miller, Communications of the ACM,
-- October 1988, Volume 31, Number 10.

m = 2147483647  -- modulus
a = 16807       -- multiplier

randoms :: Integer -> [Integer]

-- Given an initial seed (any integer value), returns a potentially
-- infinite list of pseudorandom numbers in [1..m-1].

randoms seed  = f (f s1 !! 2)           -- gives a `better' initial z than f s1
    where s0  = abs seed `mod` m        -- s0 in [0..m-1]
          s1  = if s0==0 then 1 else s0 -- s1 in [1..m-1]
          f z = z' : f z'               where z' = (a * z) `mod` m

random :: Integer -> Integer -> Integer

-- Given a lower and upper bound (x <= y), and a pseudorandom number
-- in [1..m-1], returns a pseudorandom number in [x..y].

random x y z = (z * (y-x+1)) `div` m + x
-- 
_______________________________________________________________________________

E.Ireland@massey.ac.nz          Evan Ireland, School of Information Sciences,
  +64 63 69099 x8541          Massey University, Palmerston North, New Zealand.
Technical Details
Message-ID: <1991May23.005659.10244@massey.ac.nz>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!uunet!munnari.oz.au!comp.vuw.ac.nz!cc-server4.massey.ac.nz!EIreland
References: <1991May20.161445@axion.bt.co.uk>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!uunet!munnari.oz.au!comp.vuw.ac.nz!cc-server4.massey.ac.nz!EIreland
From: news@massey.ac.nz (USENET News System)
Newsgroups: comp.lang.functional
Subject: Re: Random Number Generating Algorithm/script
Message-ID: <1991May23.005659.10244@massey.ac.nz>
Date: 23 May 91 00:56:59 GMT
References: <1991May20.161445@axion.bt.co.uk>
Organization: School of Maths and Info. Sci., Massey University, NZ
Lines: 52