Skip to content

Commit

Permalink
Add XORShift Seed
Browse files Browse the repository at this point in the history
New modulo random, a simple random using modulus
Add XORShift Seed, based on research by George Marsaglia
  • Loading branch information
miputra committed Dec 6, 2021
1 parent 3908daf commit 10002b4
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 2 deletions.
2 changes: 2 additions & 0 deletions RandEXom/RandEXom.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@
<Compile Include="Interface\IRandomR.cs" />
<Compile Include="Interface\ISeedR.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RandomLib\ModuloRandom.cs" />
<Compile Include="RandomLib\NetRandom.cs" />
<Compile Include="RandomLib\SSRNGRandom.cs" />
<Compile Include="SeedLib\IterativeSeedR.cs" />
<Compile Include="SeedLib\IterativeSeedRCustom.cs" />
<Compile Include="SeedLib\LCGSeedR.cs" />
<Compile Include="SeedLib\SeedR.cs" />
<Compile Include="SeedLib\XORShiftSeed.cs" />
<Compile Include="Utility\LongR.cs" />
<Compile Include="Utility\TypeR.cs" />
</ItemGroup>
Expand Down
53 changes: 53 additions & 0 deletions RandEXom/RandomLib/ModuloRandom.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RandEXom.RandomLib
{
/// <summary>
/// Simple random with output of seed modulo
/// </summary>
public class ModuloRandom : RandEXom.Interface.IRandomR
{
private RandEXom.Interface.ISeedR seed;
public ModuloRandom(long? seed = null)
{
this.seed = new SeedLib.XORShiftSeed(seed);
}

public ModuloRandom(Interface.ISeedR seed)
{
this.seed = seed;
}

public string GetSeed()
{
return seed.init.ToString();
}

public void NextBytes(byte[] buffers)
{
for (int i = 0; i < buffers.Length; i++)
{
buffers[i] = (byte)Math.Abs((seed.now));
seed.Next();
}
}

public int NextInt(int min, int max)
{
int val = min + (int)Math.Abs(seed.now % (max - min));
seed.Next();
return val;
}

public long NextLong(long min, long max)
{
long val = min + Math.Abs((seed.now % (max - min)));
seed.Next();
return val;
}
}
}
2 changes: 0 additions & 2 deletions RandEXom/SeedLib/SeedR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ namespace RandEXom.SeedLib
class SeedR : RandEXom.Interface.ISeedR
{
private long _seed = 0;
private long currentSeed = 0;
private long previousSeed = 0;

public long init
{
Expand Down
65 changes: 65 additions & 0 deletions RandEXom/SeedLib/XORShiftSeed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RandEXom.SeedLib
{
/// <summary>
/// Based on research by George Marsaglia https://www.jstatsoft.org/article/view/v008i14
/// </summary>
public class XORShiftSeed : RandEXom.Interface.ISeedR
{

private long _seed = 0;
public long init
{
get
{
return _seed;
}
}

public long previous
{
get
{
return previousSeed;
}
}

public long now
{
get
{
return currentSeed;
}
}
private long currentSeed = 0;
private long previousSeed = 0;

public XORShiftSeed(long? seed = null)
{
long new_seed = 0;
if (seed == null)
new_seed = Utility.LongR.GetJoinedCurrentDate();
else
new_seed = (long)seed;
this._seed = new_seed;
this.currentSeed = new_seed;
this.previousSeed = new_seed;
//this.m = m;
}

public void Next()
{
previousSeed = currentSeed;
ulong c = unchecked((ulong)(currentSeed - long.MinValue));
c ^= c << 13;
c ^= c >> 7;
c ^= c << 17;
currentSeed = unchecked((long)c + long.MinValue);
}
}
}
19 changes: 19 additions & 0 deletions RandomisatorOutputTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ static void Main(string[] args)
Console.WriteLine("[3] to test pong");
Console.WriteLine("[4] to test distance");
Console.WriteLine("[5] to test SSRNG");
Console.WriteLine("[6] to test XORShift");
string res = Console.ReadLine();
switch (res)
{
Expand All @@ -39,6 +40,9 @@ static void Main(string[] args)
case "5":
TestSSRNG();
break;
case "6":
TestXORShift();
break;
default:
return;
}
Expand Down Expand Up @@ -167,6 +171,21 @@ static void TestSSRNG()

Console.WriteLine("Test long");

for (int i = 0; i < 10; i++)
{
Console.WriteLine(rand.NextLong(0, 10000));
}
}
static void TestXORShift()
{
ModuloRandom rand = new ModuloRandom();
for (int i = 0; i < 10; i++)
{
Console.WriteLine(rand.NextInt(0, 100));
}

Console.WriteLine("Test long");

for (int i = 0; i < 10; i++)
{
Console.WriteLine(rand.NextLong(0, 10000));
Expand Down

0 comments on commit 10002b4

Please sign in to comment.