std

source of std::random

1// Pseudo-random numbers: xoshiro256** (Blackman and Vigna), fast and2// statistically strong, not for secrets.3//4// A generator from a seed gives the same sequence on every run and every5// platform, the interpreter included; one from entropy is seeded by the6// system's random source (alloyc/src/support/system.c).78import std::number;9import std::math;1011extern al_sys_entropy(buffer: &var [u8], length: u64) -> i32;12extern al_sys_monotonic() -> u64;1314exp type Random = struct {15    state: [u64 : 4]16};1718// A generator whose sequence the seed decides, expanded by SplitMix64.19exp fn Random::from_seed(seed: u64) -> Random {20    var mix = seed;21    var state: [u64 : 4] = [0, 0, 0, 0];22    for ([..4]) |k| {23        mix = mix +% 0x9E3779B97F4A7C15;24        var z = mix;25        z = (z ^ (z >> 30)) *% 0xBF58476D1CE4E5B9;26        z = (z ^ (z >> 27)) *% 0x94D049BB133111EB;27        state[k] = z ^ (z >> 31);28    }29    return Random { .state = state };30}3132// A generator seeded by the system's random source, or by the clock when33// there is none.34exp fn Random::from_entropy() -> Random {35    var bytes: [u8 : 8] = [0 : 8];36    if (al_sys_entropy(&var bytes[..], 8) != 0) {37        return Random::from_seed(al_sys_monotonic());38    }39    return Random::from_seed(bytes as u64);40}4142// The next 64 random bits.43exp fn next(self r: &var Random) -> u64 {44    const s = &var r.state;45    const result = rotate(s[1] *% 5, 7) *% 9;46    const t = s[1] << 17;47    s[2] = s[2] ^ s[0];48    s[3] = s[3] ^ s[1];49    s[1] = s[1] ^ s[2];50    s[0] = s[0] ^ s[3];51    s[2] = s[2] ^ t;52    s[3] = rotate(s[3], 45);53    return result;54}5556// A number in [0, bound), every one equally likely (Lemire's method); 057// for a bound of 0.58exp fn below(self r: &var Random, bound: u64) -> u64 {59    if (bound == 0) {60        return 0;61    }62    var product = wide_multiply(r.next(), bound);63    if (product.low < bound) {64        const threshold = (0 -% bound) % bound;65        while (product.low < threshold) {66            product = wide_multiply(r.next(), bound);67        }68    }69    return product.high;70}7172// A number in [low, high), integer or float; low when the range is empty.73exp fn range<T: Number>(self r: &var Random, low: T, high: T) -> T {74    if (!(low < high)) {75        return low;76    }77    if (T: Float) {78        return low + (high - low) * (r.unit() to T);79    }80    if (low >= 0) {81        const base = low to u64;82        return (base + r.below((high to u64) - base)) to T;83    }84    const base = low to i64;85    const span = (high to i64) -% base;86    return (base +% (r.below(span as u64) as i64)) to T;87}8889// A float in [0, 1), with 53 random bits.90exp fn unit(self r: &var Random) -> f64 {91    return ((r.next() >> 11) to f64) * 0.00000000000000011102230246251565; // 2^-5392}9394// True with the given probability.95exp fn chance(self r: &var Random, probability: f64) -> bool {96    return r.unit() < probability;97}9899// Puts the items in a random order, every order equally likely100// (Fisher-Yates).101exp fn shuffle<T>(self r: &var Random, items: &var [T]) {102    var k = items.length();103    while (k > 1) {104        const j = r.below(k);105        k -= 1;106        if (j != k) {107            var t = move items[k];108            items[k] = move items[j];109            items[j] = move t;110        }111    }112}113114fn rotate(x: u64, k: u64) -> u64 {115    return (x << k) | (x >> (64 - k));116}117118type Wide = struct {119    high: u64,120    low: u64121};122123// The 128-bit product, from 32-bit halves.124fn wide_multiply(a: u64, b: u64) -> Wide {125    const a_low = a & 0xFFFFFFFF;126    const a_high = a >> 32;127    const b_low = b & 0xFFFFFFFF;128    const b_high = b >> 32;129    const low_low = a_low * b_low;130    const high_low = a_high * b_low;131    const low_high = a_low * b_high;132    const high_high = a_high * b_high;133    const middle = (low_low >> 32) + (high_low & 0xFFFFFFFF) + (low_high & 0xFFFFFFFF);134    return Wide {135        .high = high_high + (high_low >> 32) + (low_high >> 32) + (middle >> 32),136        .low = (middle << 32) | (low_low & 0xFFFFFFFF)137    };138}