proper nextdouble

direct randombool
thread safe parallelized timer changes
This commit is contained in:
Mark Sturgill 2013-10-13 19:27:25 -07:00
parent 41019b691b
commit b3831721d9
3 changed files with 163 additions and 84 deletions

View file

@ -46,23 +46,28 @@ namespace Server {
get { return _Random.GetType(); }
}
public static double NextDouble() {
return _Random.NextDouble();
}
public static int Next(int c) {
return _Random.Next(c);
}
public static bool NextBool() {
return _Random.NextBool();
}
public static void NextBytes(byte[] b) {
_Random.NextBytes(b);
}
public static double NextDouble() {
return _Random.NextDouble();
}
}
public interface IRandomImpl {
double NextDouble();
int Next(int c);
bool NextBool();
void NextBytes(byte[] b);
double NextDouble();
}
public interface IHardwareRNG {
@ -75,13 +80,6 @@ namespace Server {
public SimpleRandom() {
}
public double NextDouble() {
double r;
lock (m_Random)
r = m_Random.NextDouble();
return r;
}
public int Next(int c) {
int r;
lock (m_Random)
@ -89,10 +87,21 @@ namespace Server {
return r;
}
public bool NextBool() {
return NextDouble() >= .5;
}
public void NextBytes(byte[] b) {
lock (m_Random)
m_Random.NextBytes(b);
}
public double NextDouble() {
double r;
lock (m_Random)
r = m_Random.NextDouble();
return r;
}
}
public sealed class CSPRandom : IRandomImpl {
@ -145,18 +154,21 @@ namespace Server {
}
}
public double NextDouble() {
byte[] b = new byte[8];
_GetBytes(b);
return (double)BitConverter.ToUInt64(b, 0) / ulong.MaxValue;
}
public int Next(int c) {
return (int)(c * NextDouble());
}
public bool NextBool() {
return (NextByte() & 1) == 1;
}
private byte NextByte() {
CheckSwap(1);
lock(_sync)
return _Working[_Index++];
}
public void NextBytes(byte[] b) {
int c = b.Length;
@ -167,6 +179,19 @@ namespace Server {
}
_GetBytes(b);
}
public double NextDouble() {
byte[] b = new byte[8];
_GetBytes(b);
/* double: 53 bits of significand precision
* ulong.MaxValue >> 11 = 9007199254740991
* 2^53 = 9007199254740992
*/
return ((double)(BitConverter.ToUInt64(b, 0) >> 11) / 9007199254740992);
}
}
public sealed class RDRand32 : IRandomImpl, IHardwareRNG {
@ -227,16 +252,21 @@ namespace Server {
}
}
public double NextDouble() {
byte[] b = new byte[8];
_GetBytes(b);
return (double)BitConverter.ToUInt64(b, 0) / ulong.MaxValue;
}
public int Next(int c) {
return (int)(c * NextDouble());
}
public bool NextBool() {
return (NextByte() & 1) == 1;
}
private byte NextByte() {
CheckSwap(1);
lock(_sync)
return _Working[_Index++];
}
public void NextBytes(byte[] b) {
int c = b.Length;
@ -246,6 +276,18 @@ namespace Server {
}
_GetBytes(b);
}
public double NextDouble() {
byte[] b = new byte[8];
_GetBytes(b);
/* double: 53 bits of significand precision
* ulong.MaxValue >> 11 = 9007199254740991
* 2^53 = 9007199254740992
*/
return ((double)(BitConverter.ToUInt64(b, 0) >> 11) / 9007199254740992);
}
}
public sealed class RDRand64 : IRandomImpl, IHardwareRNG {
@ -306,16 +348,21 @@ namespace Server {
}
}
public double NextDouble() {
byte[] b = new byte[8];
_GetBytes(b);
return (double)BitConverter.ToUInt64(b, 0) / ulong.MaxValue;
}
public int Next(int c) {
return (int)(c * NextDouble());
}
public bool NextBool() {
return (NextByte() & 1) == 1;
}
private byte NextByte() {
CheckSwap(1);
lock(_sync)
return _Working[_Index++];
}
public void NextBytes(byte[] b) {
int c = b.Length;
@ -325,6 +372,18 @@ namespace Server {
}
_GetBytes(b);
}
public double NextDouble() {
byte[] b = new byte[8];
_GetBytes(b);
/* double: 53 bits of significand precision
* ulong.MaxValue >> 11 = 9007199254740991
* 2^53 = 9007199254740992
*/
return ((double)(BitConverter.ToUInt64(b, 0) >> 11) / 9007199254740992);
}
}
public enum RDRandError : int {