You can write your topic however you want, but you need to answer these questions:
Im making a luck based incremental game and when i have more than 10 rarities i get the error “invalid argument #2 to ‘random’ (interval is empty)”
i have tried fixing it myself and gotten help from 3 other developers
i know what the error is but i dont understand why i get it when my first number is 1 and second is 190734863281
function module.getRarity()
local RarityTable = require(game.ReplicatedStorage.Rarities).Rarities
local chances = {}
--[[for i, v in pairs(RarityTable) do
chances[i] = 5 ^ (9 - i)
end]]
for i = #RarityTable, 1, -1 do
chances[i] = 5 ^ (17-i)
end
local TotalWeight = 0;
for i, v in pairs(chances) do
TotalWeight += v
end
print(TotalWeight)
for i, v in pairs(RarityTable) do
local ChosenRarity = math.random(1, TotalWeight)
TotalWeight -= chances[i]
if ChosenRarity > TotalWeight then
return RarityTable[i], chances
end
end
end
It’s not possible to generate random numbers in Lua that are higher than the maximum value of a 32-bit integer using the math.random function (the maximum value is 2,147,483,647).
The number you have (190734863281) is higher than the maximum value.
To fix this issue, you can replace the math.random function with the Random.new function.
The Random.new constructor creates a new instance of a pseudo-random number generator that can generate random numbers within a specified range, including ranges that exceed the maximum value of a 32-bit integer.
The upper bound of the range that can be used with Random.new is determined by the data type used to represent the upper bound. In Lua, the number data type is implemented as a double-precision floating-point number, which has a maximum value of approximately 1.79 × 10^308.
However, it’s important to note that the quality of the randomness of the numbers generated by a pseudo-random number generator can degrade as the range of the generated numbers increases. Therefore, generating very large random numbers may not provide the same level of randomness or unpredictability as generating smaller random numbers.
If you wish to keep using the math.random function, you need to limit the TotalWeight value to something lower than the maximum value.
OP is currently choosing from a range between 1 and TotalWeight.
In order for the math.random function to work as intended, TotalWeight needs to be equal to or above 1.
might try to prevent it from becoming negative with math.clamp(0,99999)
just do math.random(totalweight, 1, 999999) then you will always have a value above 1 so you dont need the if and the script will always give the lowest rarity if the weight was negative
The math.random function takes 2 arguments, not 3.
math.random(minRange, maxRange)
Here’s an example of how you can implement what you suggested:
local min = 1
local max = 10
local clamp_min = 3
local clamp_max = 7
local random_num = math.random(min, max)
local clamped_num = math.clamp(random_num, clamp_min, clamp_max)
print(clamped_num)
math.random(min, max) generates a random number between 1 and 10, and math.clamp(random_num, clamp_min, clamp_max) clamps the result to the range between 3 and 7.
It’s because the math.random() function breaks when the numbers break the 32-bit barrier, i.e. when they go above 2147483647 (the integer limit), anything beyond this will cause a wrap around to negative values as the bits drop off the end of the 32nd bit. Try this code:
math.random(1,2147483647);
math.random(1,2147483648); -- one beyond integer upper limit
The second call will error, so even though the function inputs say ‘number’ as type, the under-the-hood function it calls is the standard library ANSI C function “rand” which is limited to integers.
There are two choices: Random.new():NextNumber() and Random.new():NextInteger().
The choice between Random.new():NextNumber() and Random.new():NextInteger() depends on what you need to use the random number for.
If you need a random number with decimal places, such as for positioning objects or determining a value that can be a fractional number, then Random.new():NextNumber() is the best choice. This method generates a random number between 0 and 1 (inclusive) with up to 15 decimal places.
If you need a random whole number (which is probably what you’re looking for), such as for generating a random index in an array or determining a random outcome with a finite number of possibilities, then Random.new():NextInteger() is the best choice. This method generates a random integer between two values that you specify.
It’s important to choose the method that best fits your needs, depending on whether you need a decimal or a whole number.