From 35934baec0e5644039ab58e2b260a1279462adbb Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Fri, 13 Oct 2023 12:29:38 -0700 Subject: [PATCH] fix random sometimes returning negative integers --- src/Random.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Random.cs b/src/Random.cs index a6fb7b7..385bfb2 100644 --- a/src/Random.cs +++ b/src/Random.cs @@ -112,7 +112,7 @@ namespace MoonTools.ECS // .NET Random API always returns a non-negative signed int. Fun! 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) @@ -130,8 +130,9 @@ namespace MoonTools.ECS public long NextInt64() { long next = NextInternal(); - next <<= 31; + next <<= 32; next |= NextInternal(); + next >>>= 1; return next; } @@ -148,6 +149,12 @@ namespace MoonTools.ECS return min + next; } + public float NextFloat() + { + var n = NextInternal(); + return ((float) n) / uint.MaxValue; + } + public double NextDouble() { var n = NextInternal();