Skip to content

missing common_random support in library #203

Description

@jeffoodchain

According to this: #62 (comment), there's no random support in rust std.

I am currently writing req but I would need things like this, so I think it might be great to have one in our lib.

use std::cell::Cell;
use std::time::SystemTime;
use std::time::UNIX_EPOCH;

fn seed() -> u64 {
    let nanos = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos() as u64;
    // xorshift64 degenerates to all zeros if seeded with zero.
    if nanos == 0 {
        0x2545_F491_4F6C_DD1D
    } else {
        nanos
    }
}

// Returns a pseudo-random bit.
pub(crate) fn random_bit() -> bool {
    thread_local! {
        static RNG_STATE: Cell<u64> = Cell::new(seed());
    }
    RNG_STATE.with(|state| {
        let mut x = state.get();
        x ^= x << 13;
        x ^= x >> 7;
        x ^= x << 17;
        state.set(x);
        (x & 1) == 1
    })
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions