-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4129139
Showing
10 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include <stdio.h> | ||
|
||
#include "simple_rng.c" | ||
|
||
int main() { | ||
|
||
for(size_t x = 0; x < 10; ++x) { | ||
printf("%lu\n", (unsigned long)SimpleRNG_rand()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <stdint.h> | ||
|
||
uint32_t SimpleRNG_rand_r(uint32_t *rng_state) { | ||
uint32_t num = *rng_state; | ||
num ^= num << 13; | ||
num ^= num >> 17; | ||
num ^= num << 5; | ||
*rng_state = num; | ||
return num; | ||
} | ||
|
||
|
||
static uint32_t global_rng_state = 42; | ||
|
||
uint32_t SimpleRNG_rand() { | ||
return SimpleRNG_rand_r(&global_rng_state); | ||
} | ||
|
||
void SimpleRNG_seed(uint32_t seed) { | ||
global_rng_state = seed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
module SimpleRNG where | ||
|
||
import Data.Int (Int32) | ||
import Data.Bits (xor, shiftL, shiftR) | ||
import System.Random (RandomGen(..)) | ||
|
||
newtype SeedState = SeedState Int32 | ||
deriving (Eq, Show, Enum, Bounded) | ||
|
||
seed :: Integral a => a -> SeedState | ||
seed = SeedState . fromIntegral | ||
|
||
rand_r :: SeedState -> (Int32, SeedState) | ||
rand_r (SeedState a) = (d, SeedState d) | ||
where | ||
b = a `xor` (shiftL a 13) | ||
c = b `xor` (shiftR b 17) | ||
d = c `xor` (shiftL c 5) | ||
|
||
instance RandomGen SeedState where | ||
next seed_state = (fromIntegral num, new_seed_state) | ||
where | ||
(num, new_seed_state) = rand_r seed_state | ||
genRange seed_state = (fromEnum (minBound `asTypeOf` seed_state), | ||
fromEnum (maxBound `asTypeOf` seed_state)) | ||
|
||
split seed_state@(SeedState num) = (seed_state', inverted_seed_state') | ||
where | ||
(_, seed_state') = next seed_state | ||
(_, inverted_seed_state') = next inverted_seed_state | ||
inverted_seed_state = SeedState (maxBound - num) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
public class Example { | ||
public static void main(String[] args) { | ||
for(int x = 0; x < 10; ++x){ | ||
System.out.println(Long.toString(SimpleRNG.rand())); | ||
} | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
public class SimpleRNG { | ||
|
||
public static class SeedState { | ||
private long data; | ||
public SeedState(){ | ||
this(42); | ||
} | ||
public SeedState(long seed) { | ||
this.data = seed; | ||
} | ||
} | ||
|
||
private static long max_32bit = (long) Math.pow(2, 32); | ||
public static long rand_r(SimpleRNG.SeedState rng_state) { | ||
long num = rng_state.data; | ||
num ^= (num << 13) % max_32bit; | ||
num ^= (num >> 17) % max_32bit; | ||
num ^= (num << 5) % max_32bit; | ||
rng_state.data = num; | ||
return num; | ||
} | ||
|
||
private static SeedState global_rng_state = new SeedState(42); | ||
|
||
public static long rand() { | ||
return rand_r(global_rng_state); | ||
} | ||
|
||
public static void seed(long seed) { | ||
global_rng_state.data = seed; | ||
} | ||
|
||
public static void main(String[] args) { | ||
for(int x = 0; x < 10; ++x){ | ||
System.out.println(Long.toString(rand())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class SeedState(object): | ||
def __init__(self, seed = 42): | ||
self.data = seed | ||
|
||
def rand_r(rng_state): | ||
num = rng_state.data | ||
num ^= (num << 13) % (2 ** 32) | ||
num ^= (num >> 17) % (2 ** 32) | ||
num ^= (num << 5) % (2 ** 32) | ||
rng_state.data = num | ||
return num | ||
|
||
|
||
__global_rng_state = SeedState(42) | ||
|
||
def rand(): | ||
global __global_rng_state | ||
return rand_r(__global_rng_state) | ||
|
||
def seed(seed): | ||
global __global_rng_state | ||
__global_rng_state = SeedState(seed) | ||
|
||
if __name__ == '__main__': | ||
for x in range(0, 10): | ||
print(rand()) |