random

Random integers, doubles, choices, and strings via the system C library.

import random
Note: Available in Bern 0.1.2 and above. The library auto-detects the OS and binds the appropriate C library (msvcrt.dll on Windows, libc.so.6 on Unix-like systems), seeding the generator once on import.

Random numbers

get_random_int() → int

A random non-negative integer from the C runtime's rand().

get_random_int()
-- Output: (some random integer)

-- a dice roll in [1, 6]
1 + (get_random_int() % 6)
-- Output: (1 to 6)
get_random_double() → double

A random double in the range [0, 1).

get_random_double()
-- Output: (e.g. 0.4172...)
get_random_number() → number

Either a random int or a random double, chosen at random.

get_random_number()
-- Output: (an int or a double)

Random selection

random_choice(list) → element

A uniformly random element from a list (or set).

random_choice(["apple", "banana", "cherry"])
-- Output: (one of the three)

Random strings

get_random_string(length) → string

A random alphanumeric string of the requested length.

get_random_string(10)
-- Output: "aB3xK9mP2q"   (example)

Putting it together

Combine selection and number generation to build a small mock-data generator:

Generating a random user record

import random
import strings

first_names = ["Ana", "Bob", "Cia", "Dan"]

def random_user() do
    name = random_choice(first_names)
    age  = 18 + (get_random_int() % 50)
    id   = get_random_string(6)
    return name + " (" + from_int(age) + ") #" + id
end

random_user()
-- Output: "Cia (34) #k2Mx9q"   (example)

Or shuffle by repeatedly drawing a random index - here picking three distinct-ish samples:

Drawing several samples

import core
import random

deck = [1..52]
hand = map([1..3], \_ -> random_choice(deck))
hand
-- Output: [27, 4, 51]   (example)