rand
Random numbers, text and choices: dice, one-time codes, unique identifiers and shuffling a list.
import rand
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)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
endSeedAuto
func SeedAuto()The example for this item is shown with the ones above: rand-Seed.salam
Int
func Int(): i64IntRange is simpler.The example for this item is shown with the ones above: rand-Seed.salam
Int32
func Int32(): intThe example for this item is shown with the ones above: rand-Seed.salam
IntN
func IntN(n: i64): i64n.The example for this item is shown with the ones above: rand-Seed.salam
IntRange
func IntRange(lo: i64, hi: i64): i64lo 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): intIntRange 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): i64digits digits, which suits SMS confirmation codes.The example for this item is shown with the ones above: rand-Seed.salam
Float
func Float(): f64import 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
endFloatRange
func FloatRange(lo: f64, hi: f64): f64lo up to, but not including, hi.The example for this item is shown with the ones above: rand-Float.salam
Bool
func Bool(): boolThe example for this item is shown with the ones above: rand-Float.salam
BoolP
func BoolP(p: f64): boolp, between zero and one.The example for this item is shown with the ones above: rand-Float.salam
Bytes
func Bytes(n: int): strn random bytes.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)
endTextFrom
func TextFrom(n: int, charset: str): strn from the characters in charset.The example for this item is shown with the ones above: rand-Text.salam
Alpha
func Alpha(n: int): strThe example for this item is shown with the ones above: rand-Text.salam
AlphaUpper
func AlphaUpper(n: int): strThe example for this item is shown with the ones above: rand-Text.salam
AlphaMixed
func AlphaMixed(n: int): strThe example for this item is shown with the ones above: rand-Text.salam
Digit
func Digit(n: int): strThe example for this item is shown with the ones above: rand-Text.salam
Alnum
func Alnum(n: int): strThe example for this item is shown with the ones above: rand-Text.salam
Text
func Text(n: int): str!@#$, for generating a test password.The example for this item is shown with the ones above: rand-Text.salam
HexBytes
func HexBytes(n: int): strn 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(): strThe example for this item is shown with the ones above: rand-Text.salam
Hash160
func Hash160(): strThe example for this item is shown with the ones above: rand-Text.salam
Hash256
func Hash256(): strThe example for this item is shown with the ones above: rand-Text.salam
UUID
func UUID(): strxxxxxxxx-xxxx-4xxx-....The example for this item is shown with the ones above: rand-Text.salam
ChoiceStr
func ChoiceStr(v: Vector<str>): strimport 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()
endChoiceInt
func ChoiceInt(v: Vector<int>): intThe example for this item is shown with the ones above: rand-Choice.salam
ChoiceChar
func ChoiceChar(s: str): strThe example for this item is shown with the ones above: rand-Text.salam
ShuffleStr
func ShuffleStr(v: Vector<str>): Vector<str>The example for this item is shown with the ones above: rand-Choice.salam
ShuffleInt
func ShuffleInt(v: Vector<int>): Vector<int>The example for this item is shown with the ones above: rand-Choice.salam