Thread View: comp.lang.functional
5 messages
5 total messages
Started by Sam Waugh
Mon, 07 Sep 1992 00:53
Random number generator for Haskell (wanted)
Author: Sam Waugh
Date: Mon, 07 Sep 1992 00:53
Date: Mon, 07 Sep 1992 00:53
15 lines
828 bytes
828 bytes
Hi. 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 its back to Pascal and C (sob, sob). Thanks for any help. Sam Waugh Department of Computer Science University of Tasmania Australia. email: waugh@cs.utas.edu.au
Re: Random number generator for Haskell (wanted)
Author: Julian Seward (DRL PhD)
Date: Mon, 07 Sep 1992 07:47
Date: Mon, 07 Sep 1992 07:47
5 lines
645 bytes
645 bytes
Try anonymous ftp to animal.cs.chalmers.se (129.16.225.30) file /pub/haskell/library/Random.hs for a random number generator. Jules
Re: Random number generator for Haskell (wanted)
Author: 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
Re: Random number generator for Haskell (wanted)
Author: Lennart Augustsson
Date: Mon, 07 Sep 1992 19:39
Date: Mon, 07 Sep 1992 19:39
65 lines
2739 bytes
2739 bytes
In article <bruce.715827183@probitas> waugh@probitas.cs.utas.edu.au (Sam Waugh) writes:
>Hi.
>
>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
>its back to Pascal and C (sob, sob).
Yes it's been done. It comes with hbc. And here it is!!
{-
This module implements a (good) random number generator.
The June 1988 (v31 #6) issue of the Communications of the ACM has an
article by Pierre L'Ecuyer called, "Efficient and Portable Combined
Random Number Generators". Here is the Portable Combined Generator of
L'Ecuyer for 32-bit computers. It has a period of roughly 2.30584e18.
Transliterator: Lennart Augustsson
-}
module Random(randomInts, randomDoubles) where
-- Use seeds s1 in 1..2147483562 and s2 in 1..2147483398 to generate
-- an infinite list of random Ints.
randomInts :: Int -> Int -> [Int]
randomInts s1 s2 =
if 1 <= s1 && s1 <= 2147483562 then
if 1 <= s2 && s2 <= 2147483398 then
rands s1 s2
else
error "randomInts: Bad second seed."
else
error "randomInts: Bad first seed."
rands :: Int -> Int -> [Int]
rands s1 s2 =
let
k = s1 `div` 53668
s1' = 40014 * (s1 - k * 53668) - k * 12211
s1'' = if s1' < 0 then s1' + 2147483563 else s1'
k' = s2 `div` 52774
s2' = 40692 * (s2 - k' * 52774) - k' * 3791
s2'' = if s2' < 0 then s2' + 2147483399 else s2'
z = s1'' - s2''
{-
z' = if z < 1 then z + 2147483562 else z
in z' : rands s1'' s2''
-}
-- Use this instead; it is a little stricter and generates much better code
in if z < 1 then z + 2147483562 : rands s1'' s2''
else z : rands s1'' s2''
-- For those of you who don't have fromInt
-- fromInt = fromInteger . toInteger
-- Same values for s1 and s2 as above, generates an infinite
-- list of Doubles uniformly distibuted in (0,1).
randomDoubles :: Int -> Int -> [Double]
randomDoubles s1 s2 = map (\x -> fromInt x * 4.6566130638969828e-10) (randomInts s1 s2)
--
-- Lennart Augustsson
[This signature is intentionally left blank.]
Re: Random number generator for Haskell (wanted)
Author: Sam Waugh
Date: Tue, 08 Sep 1992 02:04
Date: Tue, 08 Sep 1992 02:04
14 lines
876 bytes
876 bytes
Thanks to everyone who posted and mailed regarding a random number generator in Haskell (hbc being the most common reference). I appreciate the time that people have taken in giving references and/or their own random number generators. Thanks again, Sam Waugh Department of Computer Science Universty of Tasmania Australia email: waugh@cs.utas.edu.au
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads