About Random

From Cuis CookBook
Jump to navigation Jump to search

Here I list several useful procedure related to randomness

Naive approach

  • Being naive here means we are not making any questions about how the random number are generated, what distribution they have, and if the sequence will be repeatable at next Cuis boot.
  • Get a random number between 1 and 10.
10 atRandom                                 "=> 8 "
  • Get a random number chosen from the sequence of integer (10, 11, 12, ..., 19, 20).
(Interval from: 10 to: 20) atRandom .       "=> 11 "
  • Get a random float between [0,1[
Random next.                                "=> 0.3395170491838441 "
  • Choose a random element from a list
#(123 'a' 'hello' 51) atRandom .         "=> 'hello' "
  • Shuffle a list of elements
#(123 'a' 'hello' 51) shuffled .         "=> #('hello' 'a' 51 123) "

Let's get a bit more sophisticated