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

How can I increase the random chance of things being selected in a group?

while true do --Constant loop
	--Selects money type
for i = #money_Type, #money_Type do
	wait(.5)
	local selected = money_Type[math.random(#money_Type)]
	print(selected)
end

In my code above I have a 'money bag type' that gets chosen at random; there's varying money bag types and I wanted to know how I could increase the chance of getting one over the other.

I'm really looking for more effient randomizing methods and one where I can control the chances of one being selected over another.

There's currently two items in my serverstorage ( inside a folder ), that this for loop looks through to choose from, but I plan on having other items in there soon enough.

Q:

How can I increase the random chance of other items in a list/group of items? And is there more efficient randomization methods other than math.random() ?

i've seen usage of tick() but I don't really know how that works

2 answers

✓ Accepted answer

a common trick is to think of random chances as percentages.. for instance, say you have 2 items, ItemA and ItemB, and you want itemB to be picked 60 percent of the time instead of 50/50 for both items, then you could get a random number between 1 and 100 then if the random number is from 1 - 60, then itemB get's picked, else itemA get's picked.. here itemB has a 60/100 percent chance of being chosen, but itemB has a 40/100.. I've used this in the past, and it seems to work well, although i've never taken time to think of possible complications..

1 comment

0

Oh, I really like this method. Thanks.

I would use a table where a certain amount of indexes are representing a fraction of the entire table.

Sample:

local Salad = 'Salad';
local Water = 'Water';

local Table1 = {
	Salad;
	Salad;
	Salad;
	Salad;
	-- Salad represents 4/5 of the table
	
	Water;
	-- Water represents 1/5 of the table
};
print(
	'Today I will eat/drink '..Table1[math.random(1,#Table1)]
)

If you run this muliple times, you will see that Salad appears more times than Water.

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