Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::time::Instant;
- struct RandInt(Instant);
- impl RandInt {
- fn new() -> RandInt {
- RandInt(Instant::now())
- }
- fn rand_u8(&self) -> u8 {
- (self.0.elapsed().subsec_nanos() % 256) as u8
- }
- }
- fn main() {
- let rng = RandInt::new();
- println!("My kind of random u8: {}", rng.rand_u8());
- let mut counts = [0; 256];
- for _ in 0..=1_000_000 {
- counts[rng.rand_u8() as usize] += 1;
- }
- for (i, &val) in (&counts).iter().enumerate() {
- println!("{} occured {}% of the time.", i, val as f64 / 1_000_000f64 * 100.0);
- }
- }
Add Comment
Please, Sign In to add comment