arcticfox1919 / LuaDardo

A Lua virtual machine written in Dart
Apache License 2.0
174 stars 33 forks source link

math.random: wrong limits #24

Open nioate opened 1 year ago

nioate commented 1 year ago

The upper limit in math.random() with 1 or 2 arguments is wrong - it should be increased by 1. (Since the dart random number generator is used, random(1,10) will produce numbers from 1 to 9 while in lua the upper limit is included.)

And also the ls.argCheck(low >= 0, 1, "interval too large"); shouldn't be there - lower limit can be <0.

novas1r1 commented 9 months ago

.. oh my god thanks for adding this issue. I spent a whole day checking why my script is not working. I figured out my dynamic number of the upper end of random can be 1. If its 1, I'll end up with math.random(1,1) which ends in a crash: "RangeError (RangeError (max): Must be positive and <= 2^32: Not in inclusive range 1..4294967296: 0) runLuaScript"

Is there any chance to fix this?

novas1r1 commented 9 months ago

Added a pr to fix this :) https://github.com/arcticfox1919/LuaDardo/pull/29

thauschildt commented 9 months ago

As a workaround, I just added the following code to my lua script:

_old_random=math.random
math.random=function(a,b) return _old_random(0,b-a+1)+a end