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

Can one add probability to math.Random()?

For example, can you make math.Random(1,10) have a higher probability to choose 1-5, rather than the same probability for all the numbers (1-10)?

5 comments

0

This is a really good question. I'm looking forward to seeing what the answer is. This is the most elegant solution that I could think of, and I'm not even sure it works properly: https://pastebin.com/3XEv1ziw

0

idk if there’s a better way but like SummerEquinox’s example you could always make a “tree” where if the number falls in between a certain range then X else if then Y. Maybe there’s a more sophisticated method using randomseed and some imaginative seed?

0

Ah, I would think it would be a helpful math function for everyone, but I doubt roblox would ever implement something like it.

0

There are a lot of different distribution functions that a random variable can take - could you be a bit more specific as to how you'd like it to look?

0

I'm just thinking that there should be a way to make it more likely that math.Random() will choose a certain number or set of numbers, but still have a chance to chose the rest (just less likely to).

1 answer

✓ Accepted answer

I dont know if you can add probability to a a math.random, but you can use it to make probabilistic events:

local gen = math.random(1,100)

if gen <= 10 then
-- 10% chances
elseif gen >= 10 and gen <= 30 then
-- 20% chances
elseif gen >= 30 and gen <= 60 then
-- 30% chances
elseif gen >= 60 and gen <= 100 then
-- 40% chances
end

2 comments

0

I know, but even then, the number is "random".

0

Add another math.random inside the ifs, for example: you could add a math.random(1,5) to a 40% and a math.random(6,10) to a 10%, using the same variable

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