Invalid argument #2 to 'random' (interval is empty)

You can write your topic however you want, but you need to answer these questions:

  1. 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)”

  2. 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

please help

1 Like

Use this instead:

local ChosenRarity = math.random(1, #TotalWeight)

In their case TotalWeight is already a number, so using the length operator on it will only cause it to error.

1 Like

Didn’t see that. Thanks for letting me know.

Edit:

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.

3 Likes

this can work, but now when negative it does nothing. might try to prevent it from becoming negative with math.clamp(0,99999)

but now when negative it does nothing

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)

math.clamp takes 3 arguments, not 2.

math.clamp(Value, minValue, maxValue)

i know i just quickly wrote it.

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.

omg! im sorry i ment the clamp again. litterpy just woke up so thats why.

1 Like

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.

You are better using Random: Random | Roblox Creator Documentation

so i should use random.new? …adwad

Random.new() is a more advanced object that provides greater customization for random number generation.

im testing it out now ty! should i use nextNumber? ive seen a lot of people using that with like RNG luck systems

1 Like

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.

I already gave this solution before @Xacima answer, I replied to his comment and I think he used my answer as his, gah!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.