Skip to content

Commit

Permalink
Initial implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
Qqwy committed Sep 15, 2018
0 parents commit 4129139
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 0 deletions.
Binary file added c_implementation/a.out
Binary file not shown.
10 changes: 10 additions & 0 deletions c_implementation/example.c
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());
}
}
21 changes: 21 additions & 0 deletions c_implementation/simple_rng.c
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;
}
32 changes: 32 additions & 0 deletions haskell_implementation/SimpleRNG.hs
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 added java_implementation/Example.class
Binary file not shown.
7 changes: 7 additions & 0 deletions java_implementation/Example.java
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 added java_implementation/SimpleRNG$SeedState.class
Binary file not shown.
Binary file added java_implementation/SimpleRNG.class
Binary file not shown.
38 changes: 38 additions & 0 deletions java_implementation/SimpleRNG.java
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()));
}
}
}
26 changes: 26 additions & 0 deletions python_implementation/SimpleRNG.py
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())

0 comments on commit 4129139

Please sign in to comment.