init
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
target/
|
||||
**/*.rs.bk
|
||||
Cargo.lock
|
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "permute"
|
||||
version = "0.1.0"
|
||||
authors = ["hellerve <veit@veitheller.de>"]
|
||||
|
||||
[dependencies]
|
4
README.md
Normal file
4
README.md
Normal file
@@ -0,0 +1,4 @@
|
||||
# permute
|
||||
|
||||
A Rust implementation of the unique random integer generation described
|
||||
[here](http://preshing.com/20121224/how-to-generate-a-sequence-of-unique-random-integers/).
|
32
src/lib.rs
Normal file
32
src/lib.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
pub struct RandomSeq {
|
||||
idx: u32,
|
||||
offs: u32,
|
||||
}
|
||||
|
||||
|
||||
fn permute_qpr(i: u32) -> u32 {
|
||||
let x = i as u64;
|
||||
let prime = 4294967291;
|
||||
if x >= prime { return x as u32; }
|
||||
|
||||
let residue = (x * x) % prime;
|
||||
if x <= prime / 2 {
|
||||
return residue as u32;
|
||||
}
|
||||
return (prime - residue) as u32;
|
||||
}
|
||||
|
||||
impl RandomSeq {
|
||||
pub fn new(base: u32, offset: u32) -> RandomSeq {
|
||||
RandomSeq {
|
||||
idx: permute_qpr(permute_qpr(base) + 0x682f0161),
|
||||
offs: permute_qpr(permute_qpr(offset) + 0x46790905),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next(&mut self) -> u32 {
|
||||
let res = permute_qpr((permute_qpr(self.idx) + self.offs) ^ 0x5bf03635);
|
||||
self.idx += 1;
|
||||
return res;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user