fix random sometimes returning negative integers

pull/5/head
cosmonaut 2023-10-13 12:29:38 -07:00
parent 855b8d6487
commit 35934baec0
1 changed files with 9 additions and 2 deletions

View File

@ -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();