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;
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?
01 | local rarities = { |
02 | Common = { probability = 699 / 1000 } ; |
03 | Uncommon = { probability = 200 / 1000 } ; -- i.e. 2/10 |
04 | Rare = { probability = 100 / 1000 } ; -- i.e. 1/10 |
05 | Legendary = { probability = 1 / 1000 } ; |
06 | } ; |
07 |
08 | function getRarity() |
09 | local probability = Random.new(tick()):NextNumber() |
10 | local cumulativeProbability = 0 |
11 | for name, item in pairs (rarities) do |
12 | cumulativeProbability = cumulativeProbability + item.probability |
13 | if probability < = cumulativeProbability then |
14 | return name, item |
15 | end |
16 | end |
17 | 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.
you can do something like this:
1 | local chance = Random.new(tick()):NextInteger( 1 , 100 ) -- This is PURE random |
2 | local rarity = |
3 | if chance = > 1 and = < 29 then |
4 | rarity = "Rare" -- 30% chance |
5 | elseif |
6 | chance = > 30 and = < 100 then |
7 | rarity = "Common" -- 70% chance |
8 | end |
9 | end |
You get the idea
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
1 | local chances = math.random( 0 , 1 ) |
2 |
3 |
4 | ------ your function |
5 | if chances < = 0.7 then |
6 | ------ dandelions |
7 | elseif chances > 0.7 then |
8 | ---- other flowers and plants |