Skip to content

Commit

Permalink
Back to actual random :D
Browse files Browse the repository at this point in the history
  • Loading branch information
FrostyCoolSlug committed Sep 12, 2024
1 parent 15b7591 commit b244016
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 21 deletions.
13 changes: 7 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions profile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ strum = { version = "0.26.3", features = ["derive"] }
ritelinked = "0.3.2"
quick-xml = "0.36.1"
byteorder = "1.5.0"
fastrand = "2.1.1"

tempfile = "3.10.1"

Expand Down
17 changes: 2 additions & 15 deletions profile/src/components/sample.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::collections::HashMap;
use std::io::Write;
use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH};

use anyhow::{bail, Result};

use enum_map::{Enum, EnumMap};
use log::warn;
use quick_xml::events::{BytesEnd, BytesStart, Event};
use quick_xml::Writer;
use ritelinked::LinkedHashMap;
Expand Down Expand Up @@ -340,19 +338,8 @@ impl SampleStack {
}

pub fn get_next_random_track(&mut self) -> Option<&Track> {
// We really don't need a 'true' random calculation here, or a massive crate that includes
// many different random implementations (see `rand`), so just instead take the current
// time in millis, and modulo the number of tracks. Should be good enough!

let track = if let Ok(duration) = SystemTime::now().duration_since(UNIX_EPOCH) {
duration.as_millis() % self.tracks.len() as u128
} else {
// Something's gone wrong getting the time, fallback to Sequential
warn!("Failure Calculating Random Track, falling back to Sequential");
return self.get_next_sequential_track();
} as usize;

Some(&self.tracks[track])
// Windows sucks at clock precision.. BACK TO ACTUAL RANDOM
Some(&self.tracks[fastrand::usize(0..self.tracks.len())])
}

pub fn get_next_sequential_track(&mut self) -> Option<&Track> {
Expand Down

0 comments on commit b244016

Please sign in to comment.