///|
test {
let r = @random.Rand::new()
inspect(r.uint(limit=100) < 100, content="true")
}///|
test {
let r = @random.Rand::chacha8(seed=b"0123456789abcdef0123456789abcdef")
let a = r.int()
let r2 = @random.Rand::chacha8(seed=b"0123456789abcdef0123456789abcdef")
let b = r2.int()
@test.assert_eq(a, b) // same seed → same sequence
}///|
test {
let r = @random.Rand::new()
// full range
let _ : Int = r.int() // [0, 2^31)
let _ : UInt = r.uint() // [0, 2^32)
let _ : Int64 = r.int64() // [0, 2^63)
let _ : UInt64 = r.uint64() // [0, 2^64)
// bounded
let die = r.int(limit=6) // [0, 6)
inspect(die >= 0 && die < 6, content="true")
let pct = r.uint(limit=100U)
inspect(pct < 100U, content="true")
}///|
test {
let r = @random.Rand::new()
let d = r.double()
inspect(d >= 0.0 && d < 1.0, content="true")
let f = r.float()
inspect(f >= (0.0 : Float) && f < (1.0 : Float), content="true")
}///|
test {
let r = @random.Rand::new()
let _ : Bool = r.boolean()
}///|
test {
let r = @random.Rand::new()
let a = [1, 2, 3, 4, 5]
r.shuffle(a.length(), fn(i, j) {
let t = a[i]
a[i] = a[j]
a[j] = t
})
a.sort()
@test.assert_eq(a, [1, 2, 3, 4, 5]) // same elements, just reordered
}///|
test {
let r = @random.Rand::new()
let big = r.bigint(128)
inspect(big >= 0N, content="true")
}///|
struct MySource {
mut value : UInt64
}
///|
impl @random.Source for MySource with fn next(self) -> UInt64 {
self.value = self.value * 6364136223846793005UL + 1UL
self.value
}
///|
test {
let gen : MySource = { value: 42 }
let r = @random.Rand::new(generator=gen as &@random.Source)
let _ = r.uint64()
}pub(open) trait Source {
fn next(Self) -> UInt64
}type Randtest {
let rand = @random.Rand::new()
let n = rand.bigint(8) // Generate random 8-bit number
inspect(n.bit_length() <= 8, content="true")
}test {
let r = @random.Rand::new()
let a : FixedArray[Int] = [1, 2, 3, 4, 5]
r.shuffle(a.length(), (i : Int, j : Int) => {
let t = a[i]
a[i] = a[j]
a[j] = t
})
}#deprecated("You may use `Rand::chacha8(seed~)` instead of `Rand::new(chacha8(seed~))")
fn chacha8(seed? : Bytes) -> &SourceInstall
Installed by default