Home > libalgo > Simple unsafe rand

Simple unsafe rand

概要

XorShift の unsafe だけどシンプルな実装.

実装

#[allow(dead_code)]
fn rand() -> u32 {
    static mut RAND_X: u32 = 123456789;
    static mut RAND_Y: u32 = 987654321;
    static mut RAND_Z: u32 = 1000000007;
    static mut RAND_W: u32 = 1145141919;
    unsafe {
        let t = RAND_X ^ (RAND_X << 11);
        RAND_X = RAND_Y;
        RAND_Y = RAND_Z;
        RAND_Z = RAND_W;
        RAND_W = (RAND_W ^ (RAND_W >> 19)) ^ (t ^ (t >> 8));
        RAND_W
    }
}