|
@@ -0,0 +1,34 @@
|
|
|
+
|
|
|
+mod random {
|
|
|
+ use std::fs::File;
|
|
|
+ use std::io::Read;
|
|
|
+
|
|
|
+ pub struct RNG {
|
|
|
+ random_device: File
|
|
|
+ }
|
|
|
+
|
|
|
+ impl RNG {
|
|
|
+ pub fn next_float(&mut self) -> f32 {
|
|
|
+ let mut buf: [u8; 4] = [0, 0, 0, 0];
|
|
|
+
|
|
|
+ self.random_device.read(&mut buf).expect("Read from /dev/random failed");
|
|
|
+
|
|
|
+ let val : f32 = u32::from_be_bytes(buf) as f32;
|
|
|
+
|
|
|
+ val as f32 / u32::max_value() as f32
|
|
|
+ }
|
|
|
+
|
|
|
+ pub fn create() -> RNG {
|
|
|
+ return RNG {
|
|
|
+ random_device: File::open("/dev/urandom").expect("Did not find source of randomness")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+fn main() {
|
|
|
+ let mut rng : random::RNG = random::RNG::create();
|
|
|
+ println!("{}", rng.next_float());
|
|
|
+}
|