Skip to content

Commit

Permalink
fixed the piece weigth generator using a correct random.NextFloat
Browse files Browse the repository at this point in the history
  • Loading branch information
ninetailsrabbit committed Jun 7, 2024
1 parent 49158d0 commit a3b17ed
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Match3Maker/Match3Maker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<PropertyGroup>
<Title>Ninetailsrabbit.Match3Maker</Title>
<Version>1.0.7</Version>
<Version>1.0.8</Version>
<Description>This lightweight library provides the core logic and functionality you need to build engaging match-3 games. Focus on game design and mechanics while leaving the complex logic to this library</Description>
<Copyright>© 2024 Ninetailsrabbit</Copyright>
<Authors>Ninetailsrabbit</Authors>
Expand Down
12 changes: 5 additions & 7 deletions Match3Maker/src/extensions/RandomExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ public static class RandomExtension {
///
/// https://stackoverflow.com/questions/63650648/random-float-in-c-sharp
/// </remarks>
public static float NextFloat(this Random random, float minValue = 0.000001f, float maxValue = 1f) {
float range = Math.Min(1, Math.Max(minValue, maxValue) - Math.Min(minValue, maxValue));
public static float NextFloat(this Random random, float minValue = float.MinValue, float maxValue = float.MaxValue) {
double range = (double)maxValue - (double)minValue;
double sample = random.NextDouble();
double scaled = (sample * range) + float.MinValue;

double mantissa = (random.NextDouble() * 2.0) - 1.0;
// choose -149 instead of -126 to also generate subnormal floats (*)
double exponent = Math.Pow(2.0, random.Next(-126, 128));

return (float)(mantissa * exponent) * range;
return (float)scaled;
}

}
Expand Down

0 comments on commit a3b17ed

Please sign in to comment.