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

How to deal with probabilities? Such as loot spawning system?

Asked by 7 years ago

How would I make a gun spawn more than another gun? IDK how to deal with these tpye of probabbilities.

0
I don't think you are allowed to post a question like this, you have to try it first PhoenixVortex_RBLX 33 — 7y
0
Question is, how do I use a script to make something happen 90% of the time while something else happens 10% of the time? is there a mathermatical function or something for this SyndicateHalo 40 — 7y
0
I don't know what you mean PhoenixVortex_RBLX 33 — 7y
0
you assign the numbers {1,2,3,4,5,6,7,8,9} to the first thing and number 0 to the other thing and then have a script pick a random number from 0 to 10 upon the death of the creature you want to drop the loot upon death Nogalo 148 — 7y

1 answer

Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

You need to use a weighted random. An explanation is on stackoverflow. Here's some code I've written, with a demo:

function WeightTotal(weights)
    total = 0
    for i = 1, #weights do
        total = total + weights[i]
    end
    return total
end
function WeightedRandom(weights, total) --returns index of random value (ex if weights are {0, 1, 0}, it would always return 2)
    local v = math.random() * total
    local i = 0
    while v >= 0 do
        i = i + 1
        v = v - weights[i]
    end
    return i
end
--Demonstration
local weights = {1, 7, 2}
local total = WeightTotal(weights)
local sum = {}
local index
local n = 1000
math.randomseed(os.time()) math.random()
for i = 1, total * n do
    index = WeightedRandom(weights, total)
    sum[index] = (sum[index] or 0) + 1
end
print("Option, Expected # Times Selected, Times Selected")
for i = 1, #weights do
    print(i, weights[i] * n, sum[i] or 0)
end

To use this in your situation, you would associate a different weapon with each index and then use WeightedRandom to select a random one.

Ad

Answer this question