A problem with math.random

  1. What do you want to achieve? Keep it simple and clear!
    Currently i’m making an game where with random you get some gens,
    but when i’m using randomizing chances i get an error here is it

  2. What is the issue? Include screenshots / videos if possible!

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? I was looking an vid on youtube and it should work

Code:

local ST = game:GetService("ServerStorage")
local ChancesModule = require(ST.ChanceModule)
local Players = game.Players
local function GetRandomGen()
	local Sum = 0
	for GenName,Chance in pairs(ChancesModule) do
		Sum += Chance
	end
	local RandomNumber = math.random(Sum)
	for GenName, Chance in pairs(ChancesModule) do
		if RandomNumber <= Chance then
			return GenName
		else
			RandomNumber -= Chance
		end
	end
end
Players.PlayerAdded:Connect(function(plr)
	print("Plr Added")
	plr.CharacterAdded:Connect(function(Char)
		print("Char Added")
		local pHead = Char:FindFirstChild("Head")
		if pHead:FindFirstChild("AdvancedGameGUI") then
			print("Advanced GUI Found")
			print(GetRandomGen())
		else
			if pHead:WaitForChild("AdvancedGameGUI") then
				print(GetRandomGen())
			end
		end
	end)
	end)

Here is Chances:

return {
	["Gen1"] = 1,
	["Gen2"] = 125,
	["Gen3"] = 1250,
	["Gen4"] = 12500,
	["Gen5"] = 125000,
	["Gen6"] = 1250000,
	["Gen7"] = 12500000,
	["Gen8"] = 125000000,
	["Gen9"] = 1250000000,
	["Gen10"] = 12500000000,
	["Gen11"] = 250000000000,
	["Gen12"] = 5000000000000,
	["Gen13"] = 100000000000000,
	["Gen14"] = 2000000000000000,
	["Gen15"] = 40000000000000000,
	["Gen16"] = 800000000000000000,
	["Gen17"] = 16000000000000000000,
	["Gen18"] = 320000000000000000000,
	["Gen19"] = 6400000000000000000000,
	["Gen20"] = 128000000000000000000000,
	["Gen21"] = 12800000000000000000000000,
	["Gen22"] = 1280000000000000000000000000,
	["Gen23"] = 128000000000000000000000000000,
	["Gen24"] = 1280000000000000000000000000000,
	["Gen25"] = 128000000000000000000000000000000,
	["Gen26"] = 12800000000000000000000000000000000,
	["Gen27"] = 1280000000000000000000000000000000000,
	["Gen28"] = 128000000000000000000000000000000000000,
	["Gen29"] = 12800000000000000000000000000000000000000,
	["Gen30"] = 1280000000000000000000000000000000000000000,
	["Gen31"] = 128000000000000000000000000000000000000000000,
	["Gen32"] = 12800000000000000000000000000000000000000000000,
	["Gen33"] = 1280000000000000000000000000000000000000000000000,
	["Gen34"] = 128000000000000000000000000000000000000000000000000,
	["Gen35"] = 12800000000000000000000000000000000000000000000000000,
	["Gen36"] = 1280000000000000000000000000000000000000000000000000000,
	["Gen37"] = 128000000000000000000000000000000000000000000000000000000,
	["Gen38"] = 12800000000000000000000000000000000000000000000000000000000,
	["Gen39"] = 1280000000000000000000000000000000000000000000000000000000000,
	["Gen40"] = 128000000000000000000000000000000000000000000000000000000000000,
	["Gen41"] = 256000000000000000000000000000000000000000000000000000000000000000,
	["Gen42"] = 512000000000000000000000000000000000000000000000000000000000000000000,
	["Gen43"] = 1024000000000000000000000000000000000000000000000000000000000000000000000,
	["Gen44"] = 2048000000000000000000000000000000000000000000000000000000000000000000000000,
	["Gen45"] = 4096000000000000000000000000000000000000000000000000000000000000000000000000000,
	["Gen46"] = 8192000000000000000000000000000000000000000000000000000000000000000000000000000000,
	["Gen47"] = 16384000000000000000000000000000000000000000000000000000000000000000000000000000000000,
	["Gen48"] = 32768000000000000000000000000000000000000000000000000000000000000000000000000000000000000,
	["Gen49"] = 65536000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,
	["Gen50"] = 131072000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
	}
1 Like

Try

math.random(1,Sum)

Already, i’m getting same error, + 1 is an argument #1, and sum is arg #2

You sure? The error says there is no argument #2

I’m sure dude, i already tested that, and it says INVALID argument not NO argument

Might be because the sum is too large. E.g. using math.huge as the second argument will output a similar error as far as I’m aware.

Hmm, although i need really big numbers, should i exactly do Sum = 1/Chance

And then randomNumber = math.random(Sum)?

Alright nvm argument 2 is still invalid

Have you checked if Sum is nil? If it isn’t, try printing it and see how big the number is.

image

math.floor(math.random()*Sum) might work

Ughh, it works but, i get most powerful gen by first time, smth wrong there i think, which chance is 1e94

If i use 1/Chance then i always get random number 0 and get gen6 always

This seems like a complicated way of producing the random result. I think you would be better of using math.random() with no args and defining your chances using numbers < 1.

Then you can just check if math.random() < chance.

Also I think it would be cleaner to read if you write your number literals using floating point convention (1250000000001.25e11 or 0.000000000125 → 1.25e-10)

2 Likes

Ugh i don’t understand a bit what you wrote, can you explain a bit more/

1 Like

So the main change would be to the GetRandomGen function.

local function GetRandomGen()
	-- Sort the chances in least likely to most likely order
	local chancesArray = {}
	for genName, chance in pairs(ChancesModule) do
		-- Im assuming here that the current values in Chances Module are given like 1 in X odds
		table.insert(chancesArray, {Name = genName, Value = 1/chance})
	end
	table.sort(chancesArray, function(a, b) return a.Value < b.Value end)

	-- Pick a random gen
	local randomNumber = math.random()
	for _, info in ipairs(chancesArray) do
		if randomNumber < info.Value then return info.Name end
	end
end

This line is what I was referring to with math.random() < chance.

Alright that works i think, lemme try to make infinite loop

Alright that works thanks, now the last thing i need to do to add amounts somewhere, do you have idea(like i need to save to datastore table i think), but i don’t understand how i should get that table from datastore