1

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

Asked by 10 months 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?

2
Answered by 10 months 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.

2
zomspi 546
10 months ago
Edited 10 months 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 335 — 10mo
1
Zero_Tsou 175
10 months ago
Edited 10 months 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)