├── .gitignore ├── src ├── main.rs └── lib.rs ├── Cargo.lock ├── Cargo.toml ├── README.md └── LICENSE.md /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | use RaceNG; 2 | fn main() { 3 | loop { 4 | let result = RaceNG::race(3, 5); 5 | println!("RNG says {:?}", result); 6 | } 7 | } -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "RaceNG" 7 | version = "1.0.3" 8 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "RaceNG" 3 | version = "1.0.3" 4 | edition = "2021" 5 | authors = ["HistidineDwarf "] 6 | license = "MIT" 7 | description = "Revolutionary, innovative, groundbreaking random number generator using race conditions" 8 | repository = "https://github.com/DvorakDwarf/RaceNG/tree/master" 9 | readme = "README.md" 10 | 11 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 12 | 13 | [dependencies] 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RaceNG 2 | Revolutionary, innovative, groundbreaking random number generator using race conditions written in Rust. I wrote this in like an hour because I thought it would be funny (it was). I should not need to tell you this is not a reliable source of RNG you should depend on. If you do end up using it for smthn, please DM me on discord, I want to know. 3 | 4 | I even published to [crates.io](https://crates.io/crates/RaceNG), which was suprisingly easy 5 | 6 | How 2 use 7 | ------------- 8 | 1. `cargo add RaceNG` 9 | 2. `let result = RaceNG::race(x, y)` 10 | 3. PROFIT 11 | 12 | Sample output: \ 13 | ![image](https://user-images.githubusercontent.com/96934612/230705035-2f49ddad-32e8-4682-bbf1-8fdc86915cb5.png) 14 | 15 | Explanation 4 nerds 16 | ------------- 17 | A race condition occurs when 2 or more threads are trying to use the same variable. 18 | 19 | Both threads try to set the variable to a certain value, but due to computer jank, they go about it at different speeds. This means that the value of the shared variable is undefined at a given time. If you print the variable it is *basically* random. 20 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 HistidineDwarf 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! RaceRNG, innovative new way of designing RNG 2 | 3 | use std::thread; 4 | use std::sync::mpsc; 5 | use std::time::Instant; 6 | 7 | ///The function will randomly decide between x and y numbers using a race condition 8 | pub fn race(x: i32, y: i32) -> i32 { 9 | let mut z = 0; 10 | 11 | let (tx1, rx1) = mpsc::channel(); 12 | let (tx2, rx2) = mpsc::channel(); 13 | 14 | thread::spawn(move || { 15 | loop { 16 | tx1.send(x); 17 | let slowdown = generate_primes(2000); 18 | } 19 | }); 20 | thread::spawn(move || { 21 | loop { 22 | tx2.send(y); 23 | let slowdown = generate_primes(2000); 24 | } 25 | }); 26 | 27 | let mut now = Instant::now(); 28 | loop { 29 | match rx1.try_recv() { 30 | Ok(v) => {z = v}, 31 | Err(_) => () 32 | } 33 | match time_check(&mut now, z) { 34 | Some(v) => {return v} 35 | None => (), 36 | } 37 | 38 | match rx2.try_recv() { 39 | Ok(v) => {z = v}, 40 | Err(_) => () 41 | } 42 | match time_check(&mut now, z) { 43 | Some(v) => {return v} 44 | None => (), 45 | } 46 | } 47 | } 48 | 49 | fn generate_primes(n: usize) -> Vec { 50 | let mut primes: Vec = Vec::new(); 51 | 52 | let count = 3; 53 | loop { 54 | let magic = ((count as f32).sqrt().round()) as usize + 1; 55 | for i in 2..magic { 56 | if count % i == 0 { 57 | break 58 | } 59 | primes.push(count); 60 | } 61 | 62 | if primes.len() > n { 63 | return primes; 64 | } 65 | } 66 | } 67 | 68 | fn time_check(now: &mut Instant, z: i32) -> Option{ 69 | let elapsed = now.elapsed(); 70 | if elapsed.as_millis() > 300 { 71 | *now = Instant::now(); 72 | return Some(z) 73 | } 74 | 75 | return None; 76 | } --------------------------------------------------------------------------------