-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New modulo random, a simple random using modulus Add XORShift Seed, based on research by George Marsaglia
- Loading branch information
Showing
5 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
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
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,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; | ||
} | ||
} | ||
} |
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
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,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); | ||
} | ||
} | ||
} |
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