Article View: comp.lang.functional
Article #128Re: Random number generator for Haskell (wanted)
From: Tim Lambert
Date: Mon, 07 Sep 1992 19:26
Date: Mon, 07 Sep 1992 19:26
40 lines
1942 bytes
1942 bytes
>>>>> On Mon, 7 Sep 1992 00:53:03 GMT, waugh@probitas.cs.utas.edu.au (Sam Waugh) said: > I was wondering if anybody has implemented a random number generator > in Haskell? I am trying to use Haskell for some neural network > applications and definitely need a good random function. Otherwise Here is one I wrote in Miranda (you didn't say if you wanted uniform or normal variates, so I give you both.) Tim >|| random.m random is given a positive integer seed and returns an infinite list of uniform random numbers between 0 and 1. We use a linear congruential generator (see "Art of Computer Programming Vol 2") > random :: num -> [num] > random = map (/modulus) . iterate f > where f x = (x*multiplier) mod modulus > multiplier = 69069 > modulus = 2^32 normal_random is given a seed and returns an infinite list of random normal variates with mean 0 and variance 1. (Box Muller method see "Art of Computer Programming Vol 2") > normal_random :: num -> [num] > normal_random = box_muller.(map f).random > where f x = 2*x - 1 box_muller takes a stream of uniform random numbers on [-1,1] and returns a stream of normally distributed random numbers. > box_muller :: [num] -> [num] > box_muller (x1:x2:xs) = x1*m:x2*m:rest, if r <= 1 > = rest, otherwise > where r = x1^2 + x2^2 > m = sqrt(-2*log r/r) > rest = box_muller xs
Technical Details
Message-ID:
<LAMBERT.92Sep7192624@nankeen.spectrum.cs.unsw.oz.au>Path:
arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!manuel!newshost.anu.edu.au!lambertReferences:
<bruce.715827183@probitas>Show Full Headers
Complete Article Headers
Newsgroups: comp.lang.functional Path: arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!manuel!newshost.anu.edu.au!lambert From: lambert@spectrum.cs.unsw.oz.au (Tim Lambert) Subject: Re: Random number generator for Haskell (wanted) In-Reply-To: waugh@probitas.cs.utas.edu.au's message of Mon, 7 Sep 1992 00:53:03 GMT Message-ID: <LAMBERT.92Sep7192624@nankeen.spectrum.cs.unsw.oz.au> Sender: news@newshost.anu.edu.au Organization: CS&E, Uni of NSW, Australia References: <bruce.715827183@probitas> Date: 7 Sep 92 19:26:24 Lines: 40