Random Numbers

Generate a list of random numbers using the roc-random package and the basic-cli platform.

Random Generators

Some languages provide a function like JavaScript’s Math.random() which can return a different number each time you call it. However, functions in Roc are guaranteed to return the same answer when given the same arguments, this has many benefits. But this also means something like Math.random couldn’t possibly be a valid Roc function! So, we use a different approach to generate random numbers in Roc.

This example uses a Generator which generates pseudorandom numbers using an initial seed value and the PCG algorithm. If the same seed is provided, then the same number sequence will be generated every time! The appearance of randomness comes entirely from deterministic math being done on that initial seed. The same is true of Math.random(), except that Math.random() silently chooses a seed for you at runtime.

Code

app [main!] {
    cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.19.0/Hj-J_zxz7V9YurCSTFcFdu6cQJie4guzsPMUi5kBYUk.tar.br",
    rand: "https://github.com/lukewilliamboswell/roc-random/releases/download/0.5.0/yDUoWipuyNeJ-euaij4w_ozQCWtxCsywj68H0PlJAdE.tar.br",
}

import cli.Stdout
import rand.Random

main! = |_args|

    # Print a list of 10 random numbers.
    numbers_str =
        random_numbers
        |> List.map(Num.to_str)
        |> Str.join_with("\n")

    Stdout.line!(numbers_str)

# Generate a list of random numbers using the seed `1234`.
# This is NOT cryptograhpically secure!
random_numbers : List U32
random_numbers =
    { value: numbers } = Random.step(Random.seed(1234), numbers_generator)

    numbers

# A generator that will produce a list of 10 random numbers in the range 25-75.
# This includes the boundaries, so the numbers can be 25 or 75.
# This is NOT cryptograhpically secure!
numbers_generator : Random.Generator (List U32)
numbers_generator =
    Random.list(Random.bounded_u32(25, 75), 10)

expect
    actual = random_numbers
    actual == [52, 34, 26, 69, 34, 35, 51, 74, 70, 39]

Output

Run this from the directory that has main.roc in it:

$ roc main.roc
52
34
26
69
34
35
51
74
70
39