Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Generate randomly with probability?

Is there a way to generate something randomly with one outcome being more probable than the other? For example, could I have one map having an 80% chance of happening, another map having a 19% chance of loading, and another one having a 1% chance of loading?

1 comment

0

num = math.random(1,10) if num < 8 then map1 elseif num >=8 then map2 etc etc

1 answer

✓ Accepted answer
local chances = {
	['variable1'] = 70, 
	['variable2'] = 20,
	['variable3'] = 10,	
	-- [variable] = chance integer 
}

local results = {}

table.foreach(chances, function(key, value)
	for i = 1, value do
		table.insert(results, key)
		print(key)
	end
end)

local random = math.random(#results)
print(random)
print(results[random])

1 comment

0

Thank you! Sorry for my ignorance on the basic functions of ROBLOX Lua, just getting started!

This discussion is preserved as it was. New answers, comments, and votes are closed.