Random integers, doubles, choices, and strings via the system C library.
import random
msvcrt.dll on Windows, libc.so.6 on Unix-like systems), seeding the generator once on import.
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)
A random double in the range [0, 1).
get_random_double() -- Output: (e.g. 0.4172...)
Either a random int or a random double, chosen at random.
get_random_number() -- Output: (an int or a double)
A uniformly random element from a list (or set).
random_choice(["apple", "banana", "cherry"]) -- Output: (one of the three)
A random alphanumeric string of the requested length.
get_random_string(10) -- Output: "aB3xK9mP2q" (example)
Combine selection and number generation to build a small mock-data generator:
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:
import core import random deck = [1..52] hand = map([1..3], \_ -> random_choice(deck)) hand -- Output: [27, 4, 51] (example)