From 33a943cbe34d831253f590ba0324affff9e04e6d Mon Sep 17 00:00:00 2001 From: r0r-5chach Date: Wed, 14 Aug 2024 02:57:06 +0100 Subject: [PATCH] readme & license --- LICENSE | 21 ++++++++++++++++ README.md | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 25 ++++++++++++------- 3 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 LICENSE create mode 100644 README.md diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e370fc1 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 r0r-5chach + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ab20bfc --- /dev/null +++ b/README.md @@ -0,0 +1,70 @@ +# ShoddyCast Graveler Soft Lock + +The inspiration for this project comes from a (YouTube video)[https://www.youtube.com/watch?v=M8C8dHQE2Ro] by ShoddyCast where he explores a soft lock scenario in the Pokémon Fire Red/Leaf Green games. + +This project simulates rolling a virtual die to determine the maximum number of consecutive rolls resulting in the number 4. The simulation is run in parallel across multiple iterations to improve performance. The project leverages the Xoshiro256++ pseudorandom number generator (PRNG) and the Rayon library for multi-threading. + +## Features + +- High-Performance Random Number Generation: Uses the Xoshiro256++ algorithm for fast and statistically sound random number generation. +- Parallel Processing: Utilizes the Rayon library to perform simulations concurrently across multiple threads, speeding up the computation. +- Detailed Timing: Measures and outputs the time taken to complete the simulation, broken down into days, hours, minutes, seconds, and milliseconds. + +## Prerequisites +- Rust: Ensure that you have Rust installed on your system. If not, you can install it from rust-lang.org. + +## Running the Code + +1. Clone the Repository: +```sh +git clone https://github.com/your-username/random-dice-simulation.git +cd random-dice-simulation +``` + +2. Build the Project: +```sh +cargo build --release +``` + +3. Run the Simulation: +```sh +cargo run --release +``` + +This will start the simulation with 1,000,000 iterations. The program will output the highest number of consecutive rolls resulting in the number 4, along with the time taken to complete the simulation. + +## Configuration + +- Total Iterations: + You can modify the total_iterations variable in the main function to change the number of simulations performed. +- Random Number Generator: + The code uses the Xoshiro256++ RNG, initialized with a seed of 1234. You can change the seed in the Xoshiro256PlusPlus::initialize_states call if needed. + +## Code Structure + +- main.rs: Contains the main simulation logic, including RNG initialization, parallel processing, and timing. +- roll_dice function: Simulates rolling a virtual die and returns a value between 0 and 3, with 4 being treated as 0 for simplicity. +- TimerResult struct: Converts and stores the duration of the simulation in a human-readable format. + +## Example Output + +```sh +Highest number of Ones: 7 (completed in 0 days, 0 hours, 0 minutes, 12 seconds, and 345 milliseconds) +``` + +## Dependencies +- gpu_rand: For Xoshiro256++ random number generation. +- rayon: For parallel processing. +- anyhow: For simplified error handling. + +## License + +This project is licensed under the MIT License - see the LICENSE file for details. + +## Contributions + +Contributions are welcome! Please fork the repository and submit a pull request with your changes. + +## Contact + +If you have any questions or issues, feel free to open an issue in the repository or contact the me at [r0r-5chach.xyz@proton.me]. diff --git a/src/main.rs b/src/main.rs index fc8bf94..f9ff112 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,8 @@ use gpu_rand::xoroshiro::{rand_core::RngCore, Xoshiro256PlusPlus}; use rayon::prelude::*; use std::time::{Duration, Instant}; -const MINS: u64 = 60; +const SECS: u64 = 1000; +const MINS: u64 = 60 * SECS; const HOURS: u64 = 60 * MINS; const DAYS: u64 = 24* HOURS; @@ -18,10 +19,10 @@ fn main() -> anyhow::Result<()> { (0..231).into_iter() //Roll dice 232 times .reduce(|count, _| //Reduce all dice rolls to a total of value 4 results match roll_dice(rng) { - 0 => count + 1 , //Increment count if 4 is rolled - _ => 0 , //Reset count to ensure value 4 rolls are consecutive + 0 => count + 1, //Increment count if 4 is rolled + _ => count , //Reset count to ensure value 4 rolls are consecutive } - ).unwrap() + ).unwrap_or(0) }).max() //Reduce all iteration results to the highest result .unwrap(); @@ -31,8 +32,9 @@ fn main() -> anyhow::Result<()> { let hours = timer.hours; let minutes = timer.minutes; let seconds = timer.seconds; + let milliseconds = timer.milliseconds; - Ok(println!("Highest number of Ones: {result} (completed in {days} days, {hours} hours, {minutes} minutes, and {seconds} seconds)")) + Ok(println!("Highest number of Ones: {result} (completed in {days} days, {hours} hours, {minutes} minutes, {seconds} seconds, and {milliseconds} milliseconds)")) } fn roll_dice(mut rng: Xoshiro256PlusPlus) -> u64 { @@ -44,25 +46,30 @@ struct TimerResult { hours: u64, minutes: u64, seconds: u64, + milliseconds: u64, } impl TimerResult { fn from_duration(duration: Duration) -> Self { - let duration = duration.as_secs(); - + let duration = duration.as_millis() as u64; + let days = duration / DAYS; let duration = duration % DAYS; - let hours = (duration % DAYS) / HOURS; + let hours = duration / HOURS; let duration = duration % HOURS; let minutes = duration / MINS; - let seconds = duration % MINS / MINS; + let duration = duration % MINS; + + let seconds = duration / SECS; + let milliseconds = duration % SECS; Self { days, hours, minutes, seconds, + milliseconds, } } }