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

Can someone teach me how to select by probability?

so lets say you have 4 options Option A Option B Option C Option D

Option A has a 50% chance of being selected, option B has a 30% chance of being selected, option C has a 10% chance of being selected, and option D also has a 10% chance of being selected

does anybody know I can do this when I call a function? like randomly select one of the four options at the given rates?

1 answer

✓ Accepted answer

This should be quite easy using math.random(). When I said that, you may have thought "That generates a whole number, not a value based on probability", but if you think outside of the box, it becomes very basic. All you would have to do is set a variable for math.random(1,10) like so:

local choice = math.random(1,10)

and tell the script that if it is 1, 2, 3, 4, or 5 (5/10 numbers aka 50%), do option A, if it is 2, 3, or 4, do option B (3/10 numbers aka 30%), etc, like so:

local choice = math.random(1,10)

if choice == 1 or choice == 2 or choice == 3 or choice == 4 or choice == 5 then
-- do option A
elseif choice == 6 or choice == 7 or choice == 8 then
-- do option B
elseif choice == 9 then
-- do option C
elseif choice == 10 then
-- do option D
end

Hope this helped.

-Cmgtotalyawesome

2 comments

2

You can simplify this by using the 'greater than or equals' and 'less than or equals' operators. if choice >= 1 and choice is <= 5 etc

0

@vector3_zero thanks, didn't think of that xD

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