Browse Source

Initial commit

Includes random number generator by using the /dev/random file.
Christoph Stelz 1 year ago
commit
192e74177e
4 changed files with 50 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 7 0
      Cargo.lock
  3. 8 0
      Cargo.toml
  4. 34 0
      src/main.rs

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+/target

+ 7 - 0
Cargo.lock

@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "monte-carlo"
+version = "0.1.0"

+ 8 - 0
Cargo.toml

@@ -0,0 +1,8 @@
+[package]
+name = "monte-carlo"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]

+ 34 - 0
src/main.rs

@@ -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());
+}