Source Code

Ceylon Random provides:

To generate random numbers, create and use an instance of DefaultRandom:

// Create a random number generator
value random = DefaultRandom();

// Print a pseudorandom Float in the range 0.0 to 1.0:
print (random.nextFloat());

Other simple methods include Random.nextBits(), Random.nextBoolean(), Random.nextByte(), and Random.nextInteger().

Random.nextElement() can be used to generate a random number within a Range:

// Sample output: 27

or select a random element from a Sequence:

print(random.nextElement(["heads", "tails"]));
// Sample output: heads

It is also possible to obtain an infinite stream of random values using the methods Random.bits(), Random.booleans(), Random.bytes(), Random.elements(), Random.floats(), and Random.integers().

For example, to simulate multiple rolls of a die:

value diceStream => random.elements([*('⚀':6)]);
// Sample output: { ⚂, ⚀, ⚀, ⚂, ⚀, ⚅, ⚁, ⚅, ⚅, ⚁ }

Finally, randomize() and randomizeInPlace() can be used to shuffle a list:

print(randomize {
    for (suit in {"♠", "♥", "♦", "♣"})
    for (rank in {"Ace", "King", "Queen", "Jack",
    suit + rank
// sample output: { ♥6, ♣Queen, ♦King, ♥King, ♣10 }
By: John Vasileff
License: Apache Software License
randomLimitsSource Codeshared randomLimits randomLimits

Provides limits relevant to generating random numbers on the current runtime.

By: John Vasileff
randomizeSource Codeshared List<Elements> randomize<Elements>({Elements*} elements, Random random = ...)

Shuffle the given elements, returning a new List.

  • random = DefaultRandom()
randomizeInPlaceSource Codeshared void randomizeInPlace<Element>(Array<Element> elements, Random random = ...)

Shuffle the given elements. This operation modifies the Array.

  • random = DefaultRandom()
RandomSource Codeshared Random

An interface for random number generators. Satisfying classes must implement Random.nextBits(), which is used by the default implementaitons of the methods of this interface.

DefaultRandomSource Codeshared final DefaultRandom

A pseudorandom number generator.

The algorithm used by this class to generate pseudorandom numbers may be platform specific and is subject to change in future versions of this module.

Currently, A Linear Congruential Generator (LCG) pseudorandom number generator is used, as defined by the recurrence relation:

Xₙ₊₁ ≡ (a Xₙ + c) (mod m)

The following parameters are used for the JVM:

a = 25214903917
c = 11
m = 2^48
output bits = 16..47

And for JavaScript:

a = 214013
c = 2531011
m = 2^32
output bits = 16..30


randomLimitsSource Codeshared randomLimits

Provides limits relevant to generating random numbers on the current runtime.