Guest User

Untitled

a guest
Jun 24th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. use std::time::Instant;
  2.  
  3. struct RandInt(Instant);
  4.  
  5. impl RandInt {
  6. fn new() -> RandInt {
  7. RandInt(Instant::now())
  8. }
  9. fn rand_u8(&self) -> u8 {
  10. (self.0.elapsed().subsec_nanos() % 256) as u8
  11. }
  12. }
  13.  
  14. fn main() {
  15. let rng = RandInt::new();
  16.  
  17. println!("My kind of random u8: {}", rng.rand_u8());
  18.  
  19. let mut counts = [0; 256];
  20.  
  21. for _ in 0..=1_000_000 {
  22. counts[rng.rand_u8() as usize] += 1;
  23. }
  24.  
  25. for (i, &val) in (&counts).iter().enumerate() {
  26. println!("{} occured {}% of the time.", i, val as f64 / 1_000_000f64 * 100.0);
  27. }
  28. }
Add Comment
Please, Sign In to add comment