fix random sometimes returning negative integers
parent
855b8d6487
commit
35934baec0
|
@ -112,7 +112,7 @@ namespace MoonTools.ECS
|
||||||
// .NET Random API always returns a non-negative signed int. Fun!
|
// .NET Random API always returns a non-negative signed int. Fun!
|
||||||
public int Next()
|
public int Next()
|
||||||
{
|
{
|
||||||
return (int) (NextInternal() >> 1); // bitshift right to get rid of signed bit
|
return (int) (NextInternal() >>> 1); // unsigned bitshift right to get rid of signed bit
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Next(int n)
|
public int Next(int n)
|
||||||
|
@ -130,8 +130,9 @@ namespace MoonTools.ECS
|
||||||
public long NextInt64()
|
public long NextInt64()
|
||||||
{
|
{
|
||||||
long next = NextInternal();
|
long next = NextInternal();
|
||||||
next <<= 31;
|
next <<= 32;
|
||||||
next |= NextInternal();
|
next |= NextInternal();
|
||||||
|
next >>>= 1;
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,6 +149,12 @@ namespace MoonTools.ECS
|
||||||
return min + next;
|
return min + next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public float NextFloat()
|
||||||
|
{
|
||||||
|
var n = NextInternal();
|
||||||
|
return ((float) n) / uint.MaxValue;
|
||||||
|
}
|
||||||
|
|
||||||
public double NextDouble()
|
public double NextDouble()
|
||||||
{
|
{
|
||||||
var n = NextInternal();
|
var n = NextInternal();
|
||||||
|
|
Loading…
Reference in New Issue