• Modules
    • By category
    • By name
    • Most popular
    • Most downloaded
    • Repository
  • Register
  • Log in
  • Help
    • Start using Ceylon Herd
    • Publish your first module
    • Module publishing guidelines
    • All about Ceylon
    • Keyboard Shortcuts

    • s Focus search module bar
      ? Open this information panel
      j Move selection down
      k Move selection up
      enter Open current selection
Module info
Name
quintesse / ceylon.random
Ceylon Random Platform Module
Description

Ceylon Random provides:

  • a pseudorandom number generator (DefaultRandom),
  • toplevel utility functions to shuffle streams or arrays (randomize and randomizeInPlace), and
  • an easy to implement interface for use by third party random number generators (Random).

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:

print(random.nextElement(1..100));
// 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)]);
print(diceStream.take(10));
// 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",
                  *(10..2)*.string})
    suit + rank
}.take(5));
// sample output: { ♥6, ♣Queen, ♦King, ♥King, ♣10 }
Category SDK

The Ceylon SDK

Last Published Aug 21, 2017
Stats Downloads (JVM): 4122
Downloads (JS): 830
Source downloads: 3034
Module links Home
Code repository
Issue tracker
Imported By
Browse
List of published versions
1.3.3 JVM JavaScript JVM: 1.2.x, 1.3.x (latest) JavaScript: Unknown (10/0) Published Aug 21, 2017 Browse
View docs
1.3.2 JVM JavaScript JVM: 1.2.x, 1.3.x (latest) JavaScript: Unknown (10/0) Published Mar 1, 2017 Browse
View docs
1.3.1 JVM JavaScript JVM: 1.2.x, 1.3.x (latest) JavaScript: 1.2.1, 1.2.2, 1.3.x (latest) Published Nov 21, 2016 Browse
View docs
1.3.0 JVM JavaScript JVM: 1.2.x, 1.3.x (latest) JavaScript: 1.2.1, 1.2.2, 1.3.x (latest) Published Sep 15, 2016 Browse
View docs
1.2.2 JVM JavaScript JVM: 1.2.x, 1.3.x (latest) JavaScript: 1.2.1, 1.2.2, 1.3.x (latest) Published Mar 11, 2016 Browse
View docs
1.2.1 JVM JavaScript JVM: 1.2.x, 1.3.x (latest) JavaScript: 1.2.1, 1.2.2, 1.3.x (latest) Published Feb 11, 2016 Browse
View docs
You must be logged in to comment.

Ceylon Herd v1.24 Copyright 2012-2022 Red Hat. About