• 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 / 1.3.3
Ceylon Random Platform Module
Category SDK

The Ceylon SDK

Backends JVM JavaScript
Maven coordinates org.ceylon-lang:ceylon.random
Compatible Ceylon release JVM: 1.2.x, 1.3.x (latest)
JavaScript: Unknown (10/0)
Published Aug 21, 2017
Stats Downloads (JVM): 1770
Downloads (JS): 274
Source downloads: 639
Authors John Vasileff
License Apache Software License
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 }
Dependencies
ceylon.language/1.3.3
Usage
  • Import
 import ceylon.random "1.3.3";
Module links Members
Imported By
Home
Code repository
Issue tracker
Browse
Download .car
Download .js
Download source archive
Download module documentation
View API documentation

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