Skip to content

Instantly share code, notes, and snippets.

@XMPPwocky
Created November 20, 2015 22:44
Show Gist options
  • Save XMPPwocky/d311c7ec4acce218b089 to your computer and use it in GitHub Desktop.
Save XMPPwocky/d311c7ec4acce218b089 to your computer and use it in GitHub Desktop.
extern crate threadpool;
struct Rng {
state: (u32, u32)
}
impl Rng {
fn random(&mut self) -> f64 {
let r0 = (18273 * (self.state.0 & 0xFFFF)) + (self.state.0 >> 16);
self.state.0 = r0;
let r1 = (36969 * (self.state.1 & 0xFFFF)) + (self.state.1 >> 16);
self.state.1 = r1;
let x = (r0 << 16) + (r1 & 0xFFFF);
let x = x as f64;
let imm = if x < 0.0 {
x + 4294967296.0f64
} else {
x
};
imm * 2.3283064365386962890625e-10
}
}
fn main() {
let count = 4u32;
let tp = threadpool::ThreadPool::new(count as usize);
let (tx, rx) = std::sync::mpsc::channel();
let outputs = vec![0.23127878946252167, 0.3485930557362735];
for i in 0..count {
let tx = tx.clone();
let outputs = outputs.clone();
tp.execute(move || {
bf_range(std::u32::MAX/count*i, std::u32::MAX/count*(i+1), outputs);
tx.send(()).unwrap();
});
}
rx.iter().take(count as usize).collect::<Vec<_>>();
}
fn bf_range(start: u32, end: u32, outputs: Vec<f64>) {
println!("Bruteforcing from {} to {}...", start, end);
let mut rng = Rng { state: (0, 0) };
// need inclusive range syntax :|
let statesample = (outputs[0] / 2.3283064365386962890625e-10) as u32;
let first_lower = statesample >> 16;
let last_lower = statesample & 0xFFFF;
'o: for guess in start..end {
let first = first_lower + ((guess & 0xFFFF) << 16);
let last = last_lower + (guess & 0xFFFF0000);
// spread it out among the two parts
rng.state = (first, last);
for &output in &outputs[1..] {
let r = rng.random();
if r != output {
continue 'o;
}
}
println!("Got it! {:?}", rng.state);
for _ in 0..20 {
println!("Predicted output: {}", rng.random());
}
}
println!("Done.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment