Module System.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 KiCS2 implementation is based on an algorithm taken from http://en.wikipedia.org/wiki/Random_number_generation. There is an assumption that all operations are implicitly executed mod 2^32 (unsigned 32-bit integers) !!! GHC computes between -2^29 and 2^29-1, thus the sequence is NOT as random as one would like.

m_w = <choose-initializer>;    /* must not be zero */
m_z = <choose-initializer>;    /* must not be zero */
uint get_random()
{
    m_z = 36969 * (m_z & 65535) + (m_z >> 16);
    m_w = 18000 * (m_w & 65535) + (m_w >> 16);
    return (m_z << 16) + m_w;  /* 32-bit result */
}

The PAKCS implementation 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]  Deterministic 
Returns a sequence of pseudorandom, integer values.
nextIntRange :: Int -> Int -> [Int]  Deterministic 
Returns a pseudorandom sequence of values between 0 (inclusive) and the specified value (exclusive).
nextBoolean :: Int -> [Bool]  Deterministic 
Returns a pseudorandom sequence of boolean values.
getRandomSeed :: IO Int  Deterministic 
Returns a time-dependent integer number as a seed for really random numbers.
shuffle :: Int -> [a] -> [a]  Deterministic 
Computes a random permutation of the given list.

Exported operations:

nextInt :: Int -> [Int]  Deterministic 

Returns a sequence of pseudorandom, integer values.

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

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

Returns a pseudorandom sequence of values between 0 (inclusive) and the specified value (exclusive).

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]  Deterministic 

Returns a pseudorandom sequence of boolean values.

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

getRandomSeed :: IO Int  Deterministic 

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]  Deterministic 

Computes a random permutation of the given list.

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