Skip to content

Commit

Permalink
add WELL512 csharp and java implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
taqu committed Mar 3, 2016
1 parent bdde92a commit 71d1659
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# Random
Pseudo Random Number Generator

# Algorithms
as in the list below.
- WELL512 [Chris Lomont, 2008](http://lomont.org/Math/Papers/2008/Lomont_PRNG_2008.pdf)

99 changes: 99 additions & 0 deletions cs/RandomWELL.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
@file RandomWELL.cs
@brief
@date 2016/03/03 create
@author sakai_takuro
*/

//----------------------------------------------------------------------------
/**
@brief RandomWELL
*/
public class RandomWELL
{
private const int N = 16;
private const uint M0 = 0x3F800000U;
private const uint M1 = 0x007FFFFFU;

private uint seed_ = 12345678;
private int index_ = 0;
private uint[] state_ = new uint[N]
{
12345678, 277759943, 96845755, 1019000778, 861248694, 580794835, 399519109, 312451493,
476862699, 301038016, 229543161, 1467734034, 307044411, 318946322, 857734790, 336374701,
};

private byte[] bytes_ = new byte[4];

public int srand()
{
return (int)seed_;
}

public void srand(int seed)
{
index_ = 0;
seed_ = (uint)(seed & 0x7FFFFFFF);
state_[0] = seed_;
for(uint i = 1; i < N; ++i) {
state_[i] = (1812433253 * (state_[i-1] ^ (state_[i-1] >> 30)) + i);
state_[i] &= 0x7FFFFFFF;
}
}

// (0.0 1.0]
public float frand()
{
uint t = rand_();
t = M0|(t&M1);
bytes_[0] = (byte)((t>> 0)&0xFF);
bytes_[1] = (byte)((t>> 8)&0xFF);
bytes_[2] = (byte)((t>>16)&0xFF);
bytes_[3] = (byte)((t>>24)&0xFF);
return System.BitConverter.ToSingle(bytes_, 0) - 0.999999881f;
}

// [0.0 1.0)
public float frand2()
{
uint t = rand_();
t = M0|(t&M1);
bytes_[0] = (byte)((t>> 0)&0xFF);
bytes_[1] = (byte)((t>> 8)&0xFF);
bytes_[2] = (byte)((t>>16)&0xFF);
bytes_[3] = (byte)((t>>24)&0xFF);
return System.BitConverter.ToSingle(bytes_, 0) - 1.0f;
}

public int rand()
{
return (int)(rand_() & 0x7FFFFFFF);
}

private uint rand_()
{
uint a, b, c, d;

a = state_[index_];
c = state_[(index_ + 13) & 15];
b = a ^ c ^ (a << 16) ^ (c << 15);
c = state_[(index_ + 9) & 15];
c ^= c >> 11;
a = state_[index_] = b ^ c;
d = a ^ ((a << 5) & 0xDA442D24U);
index_ = (index_ + 15) & 15;
a = state_[index_];
state_[index_] = a ^ b ^ d ^ (a << 2) ^ (b << 18) ^ (c << 28);
return state_[index_];
}

public float Range(float min, float max)
{
return (min - max) * frand() + min;
}

public int Range(int min, int max)
{
return (int)((min - max) * frand2() + min);
}
}
26 changes: 26 additions & 0 deletions cs/test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Random
{
class Program
{
static void Main(string[] args)
{
using(System.IO.FileStream fileStream = new System.IO.FileStream("random_csharp.bin", System.IO.FileMode.OpenOrCreate))
using(System.IO.BinaryWriter bwriter = new System.IO.BinaryWriter(fileStream))
{
RandomWELL random = new RandomWELL();
random.srand(7654321);
for(int i=0; i<1024*1024; ++i){
bwriter.Write(random.frand());
}
bwriter.Flush();
bwriter.Close();
}
}
}
}
79 changes: 79 additions & 0 deletions java/RandomWELL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

public class RandomWELL
{
private static final int N = 16;
private static final int M0 = 0x3F800000;
private static final int M1 = 0x007FFFFF;

private static int seed_ = 12345678;
private static int index_ = 0;
private static int[] state_ = new int[]
{
12345678, 277759943, 96845755, 1019000778, 861248694, 580794835, 399519109, 312451493,
476862699, 301038016, 229543161, 1467734034, 307044411, 318946322, 857734790, 336374701,
};

public int srand()
{
return seed_;
}

public void srand(int seed)
{
seed_ = seed;
index_ = 0;
state_[0] = seed_ & 0x7FFFFFFF;
for(int i = 1; i < N; ++i) {
state_[i] = (1812433253 * (state_[i-1] ^ (state_[i-1] >> 30)) + i);
state_[i] &= 0x7FFFFFFF;
}
}

// (0.0 1.0]
public float frand()
{
int t = rand_();
t = M0 | (t & M1);
return Float.intBitsToFloat(t) - 0.999999881f;
}

// [0.0 1.0)
public float frand2()
{
int t = rand_();
t = M0 | (t & M1);
return Float.intBitsToFloat(t) - 1.0f;
}

public int rand()
{
return (int)(rand_() & 0x7FFFFFFF);
}

private int rand_()
{
int a, b, c, d;

a = state_[index_];
c = state_[(index_ + 13) & 15];
b = a ^ c ^ (a << 16) ^ (c << 15);
c = state_[(index_ + 9) & 15];
c ^= c >>> 11;
a = state_[index_] = b ^ c;
d = a ^ ((a << 5) & 0xDA442D24);
index_ = (index_ + 15) & 15;
a = state_[index_];
state_[index_] = a ^ b ^ d ^ (a << 2) ^ (b << 18) ^ (c << 28);
return state_[index_];
}

public float Range(float min, float max)
{
return (min - max) * frand() + min;
}

public int Range(int min, int max)
{
return (int)((min - max) * frand2() + min);
}
}
33 changes: 33 additions & 0 deletions java/test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.io.IOException;
import java.nio.ByteBuffer;


public class test {

/**
* @param args
*/
public static void main(String[] args)
{
try {
RandomWELL random = new RandomWELL();
random.srand(7654321);
byte[] bytes = ByteBuffer.allocate(4).array();
java.io.FileOutputStream file = new java.io.FileOutputStream("random_java.bin", false);
for(int i=0; i<1024*1024; ++i){
float f = random.frand();
int r = Float.floatToIntBits(f);
bytes[0] = (byte) ((r>>0)&0xFF);
bytes[1] = (byte) ((r>>8)&0xFF);
bytes[2] = (byte) ((r>>16)&0xFF);
bytes[3] = (byte) ((r>>24)&0xFF);
file.write(bytes);
}
file.flush();
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

0 comments on commit 71d1659

Please sign in to comment.