I need to generate numbers but the range is infinite.
I have tried this:
local num = math.random(1, math.huge)
print(num)
But it throwed this error:
ServerScriptService.Script:6: invalid argument #2 to ‘random’ (interval is empty)
I need to generate numbers but the range is infinite.
I have tried this:
local num = math.random(1, math.huge)
print(num)
But it throwed this error:
ServerScriptService.Script:6: invalid argument #2 to ‘random’ (interval is empty)
It isnt really Possible, and there are limitations to the pseudorandom number generation.
math.random()
has an Integer Limit of (2^31) - 1
or (2147483647)
, Around 2.1 Billion, which the exact number for the 32-Bit Integer Limit.
Random.new()
has an Integer Limit of (2^63) - 1
or (9223372036854776000)
, which is 9.2 Quintillion, or in other words, the 64-Bit Integer Limit.
These numbers are Important because the are the exact limits that these pseudorandom number generators can handle, and going over these limits will result in a Integer Overflow, Typically the error occurs because the number given in math.random()
is going over the 32-Bit Integer Limit, that It becomes a Negative Number, The Second Argument Cannot be Negative, so it throws the error.
Edit: This post goes into some detail on the mechanics of the method below.
Complementing the detailed response by @DasKairo , an implementation of Random.new()
would look something like this:
rand = Random.new()
num = rand:NextInteger(1, 1e90)
print(num)
So basically if I wanted to generate the number “without range”
Will this work?
local num = math.random(-2147483647, 2147483647)
or
local ran = Random.new()
ran:NextNumber(-9223372036854776000, 9223372036854776000)
It should, as they arent going past any limits, but if you just want infinite, use math.huge
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.