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

How would i go about doing math.random but with rarity chances?.

Asked by 3 years ago

QUESTION 1

I'm making a game like MM2 - short for 'murderer mystery 2', which is a murder game.

You can buy cases, but i want a certain class of item to be rarer of the other;

QUESTION 2

How do i make a percentage of something.

Like if i want 70% of the acre to be filled with dandelions and the other 30% filled with other flowers and plants?

3 answers

Log in to vote
2
Answered by
Wiscript 622 Moderation Voter
3 years ago
local rarities = {
    Common = { probability = 699/1000 };
    Uncommon = { probability = 200/1000 }; -- i.e. 2/10
    Rare = { probability = 100/1000 }; -- i.e. 1/10
    Legendary = { probability =   1/1000 };
};

function getRarity()
    local probability = Random.new(tick()):NextNumber()
    local cumulativeProbability = 0
    for name, item in pairs(rarities) do
        cumulativeProbability = cumulativeProbability + item.probability
        if probability <= cumulativeProbability then
            return name, item
        end
    end
end

So here, you want the probabilities to add up to 1.

IE: 699/1000 + 200/1000 + 100/1000 + 1/1000 = 1

So if you were to change the probabilities, you need to make sure that the probabilities add up to 1.

You may notice how the code is written as 100/1000 instead of 1/10 for example. This is because it's a lot easier to see the distribution of things when all items have a common denominator.

Ad
Log in to vote
2
Answered by
zomspi 541 Moderation Voter
3 years ago
Edited 3 years ago

you can do something like this:

local chance = Random.new(tick()):NextInteger(1,100) -- This is PURE random
local rarity = 
if chance =>1 and =<29 then
rarity = "Rare" -- 30% chance
elseif 
chance =>30 and =<100 then
rarity = "Common" -- 70% chance
end
end

You get the idea

0
I really like this answer, but unfortunately I'm going to choose Rodrigo's answer. Hope you understand :), I'll upvote your answer anyways maxpax2009 340 — 3y
Log in to vote
1
Answered by
Zero_Tsou 175
3 years ago
Edited 3 years ago

I have the answer For the 2nd question, And if you go a little creative with it, you might answer the 1st question By Yourself

local chances = math.random(0,1)


------ your function
if chances <= 0.7 then
------ dandelions
elseif chances >0.7 then
---- other flowers and plants
0
Your answer is very good! I'm impressed, I'll think of which one to answer. maxpax2009 340 — 3y
0
Unfortunately i was most satisfied with; Rodrigo's answer. I'll up-vote anyways :) maxpax2009 340 — 3y

Answer this question