Standard library · Numbers and collections

rand

Random numbers, text and choices: dice, one-time codes, unique identifiers and shuffling a list.

import rand
On this page
  1. Functions

The rand package is for games, simulations, sample data and generating identifiers. Call SeedAuto once before anything else, so that each run of the program gets different numbers. If you want the same result every time, in a test for instance, call Seed with a fixed number instead.

This package is not built for cryptography. For keys, session tokens and passwords, use the crypto package.

The examples on this page print properties of the values, such as whether they fall in a range, rather than the values themselves, because a random number differs on every run.

Functions

Seed

func Seed(s: i64)
Starts the generator from a fixed number. With the same seed the sequence of numbers is always the same.
rand-Seed.salam
import rand

func main:
    rand.Seed(42)
    a := rand.IntRange(1, 100)
    rand.Seed(42)
    b := rand.IntRange(1, 100)
    println a == b
    rand.SeedAuto()
    die := rand.Int32Range(1, 6)
    println die >= 1 and die <= 6
    n := rand.Int()
    n32 := rand.Int32()
    println rand.IntN(10) < 10, n == n, n32 == n32
    println rand.IntWithLength(6) >= 100000
end
Outputtrue true true true true true

SeedAuto

func SeedAuto()
Starts the generator from the current time, so that every run gets different numbers.

The example for this item is shown with the ones above: rand-Seed.salam

Int

func Int(): i64
A random 64-bit whole number. For a number in a particular range, IntRange is simpler.

The example for this item is shown with the ones above: rand-Seed.salam

Int32

func Int32(): int
A random 32-bit whole number.

The example for this item is shown with the ones above: rand-Seed.salam

IntN

func IntN(n: i64): i64
A random number from zero to one less than n.

The example for this item is shown with the ones above: rand-Seed.salam

IntRange

func IntRange(lo: i64, hi: i64): i64
A random number from lo to hi, both included.

The example for this item is shown with the ones above: rand-Seed.salam

Int32Range

func Int32Range(lo: int, hi: int): int
Like IntRange with a 32-bit result; for a die, 1 to 6.

The example for this item is shown with the ones above: rand-Seed.salam

IntWithLength

func IntWithLength(digits: int): i64
A random number with exactly digits digits, which suits SMS confirmation codes.

The example for this item is shown with the ones above: rand-Seed.salam

Float

func Float(): f64
A random decimal number from zero up to, but not including, one.
rand-Float.salam
import rand

func main:
    a := rand.Float()
    println a >= 0.0 and a < 1.0
    b := rand.FloatRange(10.0, 20.0)
    println b >= 10.0 and b < 20.0
    println rand.BoolP(1.0), rand.BoolP(0.0)
    flag := rand.Bool()
    println flag == true or flag == false
end
Outputtrue true true false true

FloatRange

func FloatRange(lo: f64, hi: f64): f64
A random decimal number from lo up to, but not including, hi.

The example for this item is shown with the ones above: rand-Float.salam

Bool

func Bool(): bool
True or false, each with probability one half.

The example for this item is shown with the ones above: rand-Float.salam

BoolP

func BoolP(p: f64): bool
Returns true with probability p, between zero and one.

The example for this item is shown with the ones above: rand-Float.salam

Bytes

func Bytes(n: int): str
n random bytes.
rand-Text.salam
import rand
import str

func main:
    println rand.Alpha(8).len(), rand.AlphaUpper(8).len(), rand.AlphaMixed(8).len()
    println rand.Digit(6).len(), rand.Alnum(12).len(), rand.Text(10).len()
    println rand.TextFrom(5, "ab").len(), str.Contains("ab", rand.ChoiceChar("ab"))
    println rand.Bytes(4).len(), rand.HexBytes(4).len()
    println rand.Hash().len(), rand.Hash160().len(), rand.Hash256().len()
    id := rand.UUID()
    println id.len(), id.substr(14, 1)
end
Output8 8 8 6 12 10 5 true 4 8 32 40 64 36 4

TextFrom

func TextFrom(n: int, charset: str): str
Builds text of length n from the characters in charset.

The example for this item is shown with the ones above: rand-Text.salam

Alpha

func Alpha(n: int): str
Text of lower case English letters.

The example for this item is shown with the ones above: rand-Text.salam

AlphaUpper

func AlphaUpper(n: int): str
Text of upper case English letters.

The example for this item is shown with the ones above: rand-Text.salam

AlphaMixed

func AlphaMixed(n: int): str
Text of upper and lower case English letters.

The example for this item is shown with the ones above: rand-Text.salam

Digit

func Digit(n: int): str
Text of digits only, for a one-time code.

The example for this item is shown with the ones above: rand-Text.salam

Alnum

func Alnum(n: int): str
Text of English letters and digits.

The example for this item is shown with the ones above: rand-Text.salam

Text

func Text(n: int): str
Text of English letters, digits and punctuation such as !@#$, for generating a test password.

The example for this item is shown with the ones above: rand-Text.salam

HexBytes

func HexBytes(n: int): str
n random bytes as hexadecimal text, two characters per byte.

The example for this item is shown with the ones above: rand-Text.salam

Hash

func Hash(): str
A random 32-character hexadecimal string.

The example for this item is shown with the ones above: rand-Text.salam

Hash160

func Hash160(): str
A random 40-character hexadecimal string, 160 bits.

The example for this item is shown with the ones above: rand-Text.salam

Hash256

func Hash256(): str
A random 64-character hexadecimal string, 256 bits.

The example for this item is shown with the ones above: rand-Text.salam

UUID

func UUID(): str
Builds a version 4 unique identifier of the form xxxxxxxx-xxxx-4xxx-....

The example for this item is shown with the ones above: rand-Text.salam

ChoiceStr

func ChoiceStr(v: Vector<str>): str
Returns a random element of a vector of strings.
rand-Choice.salam
import rand

func main:
    mut names := Vector {} as Vector<str>
    defer names.free()
    names.push("Ali")
    names.push("Sara")
    names.push("Reza")
    winner := rand.ChoiceStr(names)
    println winner == "Ali" or winner == "Sara" or winner == "Reza"
    mut numbers := Vector {} as Vector<int>
    defer numbers.free()
    repeat 1 to 5 in i:
        numbers.push(i)
    end
    println rand.ChoiceInt(numbers) <= 5
    shuffled := rand.ShuffleInt(numbers)
    mut total := 0
    each n in shuffled:
        total += n
    end
    println shuffled.len(), total
    println rand.ShuffleStr(names).len()
end
Outputtrue true 5 15 3

ChoiceInt

func ChoiceInt(v: Vector<int>): int
Returns a random element of a vector of numbers.

The example for this item is shown with the ones above: rand-Choice.salam

ChoiceChar

func ChoiceChar(s: str): str
Returns a random character from a string.

The example for this item is shown with the ones above: rand-Text.salam

ShuffleStr

func ShuffleStr(v: Vector<str>): Vector<str>
Returns a shuffled copy of a vector of strings, leaving the vector itself alone.

The example for this item is shown with the ones above: rand-Choice.salam

ShuffleInt

func ShuffleInt(v: Vector<int>): Vector<int>
Returns a shuffled copy of a vector of numbers.

The example for this item is shown with the ones above: rand-Choice.salam