Skip to content

Commit

Permalink
Define the Xor Shift Random Generator
Browse files Browse the repository at this point in the history
  • Loading branch information
oovm committed Dec 11, 2023
1 parent 49bdbfb commit 78f0e40
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,55 +19,3 @@ extends LinearCongruential {
self.seed
}
}

extends XorShift {
constructor(seed: u32?) {
self.state = seed ?? random_seed_safe() as u32;
}
random(mut self): u32 {
self.state.bit_xor_assign(self.state.bit_shift_left(13));
self.state.bit_xor_assign(self.state.bit_shift_right(17));
self.state.bit_xor_assign(self.state.bit_shift_left(5));
self.state
}
}

/// MT19937
structure MersenneTwister32 {
seed: u64,
index: u64,
state: [u64; 624],
}

extends MersenneTwister32 {
constructor(seed: u64?) {
self.seed = seed ?? random_seed_safe();
self.index = 0;
self.state = [0; 624];
self.state[0] = self.seed;
for i in 1..624 {
self.state[i] = 1812433253 * (self.state[i - 1] ^ (self.state[i - 1] >> 30)) + i;
}
}
random(): u64 {
if self.index == 0 {
self.generate();
}
let mut y = self.state[self.index];
y ^= y >> 11;
y ^= (y << 7) & 2636928640;
y ^= (y << 15) & 4022730752;
y ^= y >> 18;
self.index = (self.index + 1) % 624;
y
}
sealed generate(): unit {
for i in range(624) {
let y = (self.state[i] & 16⁂80000000) + (self.state[(i + 1) % 624] & 16⁂7fffffff);
self.state[i] = self.state[(i + 397) % 624] ^ (y >> 1);
if y % 2 != 0 {
self.state[i] ^= 16⁂9908b0df;
}
}
}
}
36 changes: 36 additions & 0 deletions projects/valkyrie-std/source/random/generator/XorShift32.vk
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

structure XorShift32 {
state: u32,
#constant
a: u32,
#constant
b: u32,
#constant
c: u32,
}

extends XorShift32 {
constructor(seed: u32?) {
self.a = 13;
self.b = 17;
self.c = 5;
self.state = seed ?? random_seed_safe().split_u32.2;
}
with_parameters(move self, a: u32, b: u32, c: u32): Self {
self.a = a;
self.b = b;
self.c = c;
self
}
#override(random_u32)
random(mut self): u32 {
self.state.bit_xor_assign(self.a.bit_shift_left(13));
self.state.bit_xor_assign(self.b.bit_shift_right(17));
self.state.bit_xor_assign(self.c.bit_shift_left(5));
self.state
}
#override(random_u32)
random(mut self): u64 {
u64::merge_u32(self.random(), self.random())
}
}
35 changes: 35 additions & 0 deletions projects/valkyrie-std/source/random/generator/XorShift64.vk
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@


structure XorShift64 {
state: u64,
#constant
a: u64,
#constant
b: u64,
#constant
c: u64,
}

extends XorShift32 {
constructor(seed: u64?) {
self.a = 13;
self.b = 7;
self.c = 17;
self.state = seed ?? random_seed_safe();
}
with_parameters(move self, a: u64, b: u64, c: u64): Self {
self.a = a;
self.b = b;
self.c = c;
self
}
random(mut self): u32 {
self.random().split_u32.2
}
random(mut self): u64 {
self.state.bit_xor_assign(self.a.bit_shift_left(13));
self.state.bit_xor_assign(self.b.bit_shift_right(17));
self.state.bit_xor_assign(self.c.bit_shift_left(5));
self.state
}
}

0 comments on commit 78f0e40

Please sign in to comment.