godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.38k stars 21.26k forks source link

GD.RandRange is broken at integer limits, always returning the same number. #94578

Open DuneTruffle opened 4 months ago

DuneTruffle commented 4 months ago

Tested versions

requires C# version as the the issue is related to C# Godot v4.3.dev5.mono - Windows 10.0.22631 - GLES3 (Compatibility) - NVIDIA GeForce GTX 1650 (NVIDIA; 31.0.15.5176) - 13th Gen Intel(R) Core(TM) i5-13600KF (20 Threads)

System information

Godot v4.3.dev5.mono - Windows 10.0.22631 - GLES3 (Compatibility) - NVIDIA GeForce GTX 1650 (NVIDIA; 31.0.15.5176) - 13th Gen Intel(R) Core(TM) i5-13600KF (20 Threads)

Issue description

C# GD.RandRange is broken when using the minimum and maximum value of the int type it returns the same number always, which is some big negative number

Steps to reproduce

get the specified version.

in ready function use the following code line, run the scene a few times. you will get the same number after the first run use GD.Print(GD.RandRange(int.MinValue, int.MaxValue));

Minimal reproduction project (MRP)

requires C# version of godot! create a new project and simply use GD.Print(GD.RandRange(int.MinValue, int.MaxValue)); in ready or anywhere.

Calinou commented 4 months ago

For GDScript use print(randi_range(pow(2, 31), pow(2, 31)))

The lower bound is the same as the upper bound here, so you'll always get the same value.

DuneTruffle commented 4 months ago

For GDScript use print(randi_range(pow(2, 31), pow(2, 31)))

The lower bound is the same as the upper bound here, so you'll always get the same value.

Oh sorry, late night brain doing its wonders! the GDScript version works, but the C# one does not. and i have tested GD,RandRange a lot.

DuneTruffle commented 4 months ago

i updated the post. the issue still remains with C# GD.RandRange

AThousandShips commented 4 months ago

Looks like it could be an overflow due to this code: https://github.com/godotengine/godot/blob/e25f3c0d38d457b15a63720240736f564ce0501b/core/math/random_pcg.cpp#L78

Frozenfire92 commented 1 week ago

Wanted to add some clarity here that this is broken for gdscript as well, for example randi_range at 32 bit signed int limits returns weird results

randi_range(-2147483647, 2147483646)