Module Random

Library for pseudo-random number generation in Curry.

This library provides operations for generating pseudo-random number sequences. For any given seed, the sequences generated by the operations in this module should be identical to the sequences generated by the java.util.Random package.

The algorithm is a linear congruential pseudo-random number generator described in Donald E. Knuth, The Art of Computer Programming, Volume 2: Seminumerical Algorithms, section 3.2.1.

Author: Sergio Antoy (with extensions by Michael Hanus)

Version: June 2017

Summary of exported operations:

nextInt :: Int -> [Int]   
Returns a sequence of pseudorandom, uniformly distributed 32-bits integer values.
nextIntRange :: Int -> Int -> [Int]   
Returns a pseudorandom, uniformly distributed sequence of values between 0 (inclusive) and the specified value (exclusive).
nextBoolean :: Int -> [Bool]   
Returns a pseudorandom, uniformly distributed sequence of boolean values.
getRandomSeed :: IO Int   
Returns a time-dependent integer number as a seed for really random numbers.
shuffle :: Int -> [a] -> [a]   
Computes a random permutation of the given list.

Exported operations:

nextInt :: Int -> [Int]   

Returns a sequence of pseudorandom, uniformly distributed 32-bits integer values. All 232 possible integer values are produced with (approximately) equal probability.

Example call:
(nextInt seed)
Parameters:
  • seed : The seed of the random sequence.

nextIntRange :: Int -> Int -> [Int]   

Returns a pseudorandom, uniformly distributed sequence of values between 0 (inclusive) and the specified value (exclusive). Each value is a 32-bits positive integer. All n possible values are produced with (approximately) equal probability.

Example call:
(nextIntRange seed n)
Parameters:
  • seed : The seed of the random sequence.
  • n : The bound on the random number to be returned. Must be positive.

nextBoolean :: Int -> [Bool]   

Returns a pseudorandom, uniformly distributed sequence of boolean values. The values True and False are produced with (approximately) equal probability.

Example call:
(nextBoolean seed)
Parameters:
  • seed : The seed of the random sequence.

getRandomSeed :: IO Int   

Returns a time-dependent integer number as a seed for really random numbers. Should only be used as a seed for pseudorandom number sequence and not as a random number since the precision is limited to milliseconds

shuffle :: Int -> [a] -> [a]   

Computes a random permutation of the given list.

Example call:
(shuffle rnd l)
Parameters:
  • rnd : random seed
  • l : lists to shuffle
Returns:
shuffled list