Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Obtaining a bool value based on percentage chance?

Asked by 4 years ago
Edited 4 years ago

Say I have variables set up like so:

local Chance = IntValue.Value -- An integer from 0 to 100
local Outcome = false

Now the goal here is to change the value of Outcome to true based on whatever Chance is, where the probability that Outcome becomes true is Chance/100.

For example: Chance = 3 so the probability that Outcome becomes true is 0.03 (or 3%).

How would I even go about doing this?

0
I personally wouldn't want to use percentages. I would try a workaround but i am not sure what you would be using this for. If you are making a type of item drop table when a monster dies, you could use math.random with > or < for different items. This can work with alot of things. Like say on math.random 1 out of 100, you could do <= 10 that can be like 10%, not sure this helps, cant put code now Silvanatri 24 — 4y
0
It's probability - percentages/fractions/decimals are all equivalent and the concept of weighting the chance is vital shadow7692 69 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

This is one of my favourite topics:

local Randomness = Random.new()
local Chance = 10
local Outcome = false

local function runChance()
    if Randomness:NextNumber() <= Chance/100 then
        Outcome = true 
    else
        Outcome = false
    end
end

runChance()

How this works:

NextNumber() gives us a number from 0 to 1 in decimals. Any chance divided by 100 will give us what we want; so 10 percent chance would also be 10/100 which is 0.1 so what we are saying is if this random number is smaller then the chance then it works.

So if you have 100 as your chance; 100 / 100 = 1, :NextNumber() will always generate a number at 1 or below 1. The higher the number, the higher the chance basically

Hope this helped! Best of luck!

0
local Randomness = Random.new() local Chance = 10 local Outcome = false local function runChance() if Randomness:NextNumber() <= Chance/100 then Outcome = true else Outcome = false end end runChance() User#30793 0 — 4y
Ad

Answer this question