# Thread: Generating Probability in a sequence

1. ## Generating Probability in a sequence

So I work on my friend's game in my spare time writing quests and events, that sort of thing. I'm working on an event now that has got me stumped trying to work out the probability of results!

The NPC needs to hand out one of twelve items at a particular frequency when another item is turned into her. The problem is three of them need to occur 20% of the time each, another three 10% each, three more at 2% each, and the last three at 1% each. I know that to have a roughly even chance of each item, the frequencies would need to be set such as:

ifrand(8) followed by 9, 10, 11, 13, 14, 17, 20, 25, 33, and ending with (50) between the last two. I probably could've figured this one out on my own (this much was in a guide) but twelve instances of 8% is a bit more straightforward than what I need.

I don't think the code for this allows decimals but even considering integers I find myself thinking in circles! Can anyone help? How would I go about working out more progs like this in the future?

2. ## Re: Generating Probability in a sequence

I really can't follow what you are saying after the 2nd paragraph.

you have a random variable that can take 12 values and is distributed as

$\{0.2, 0.2, 0.2, 0.1, 0.1, 0.1, 0.02, 0.02, 0.02, 0.01, 0.01, 0.01\}$

right off we have a problem in that the probabilities don't sum to 1, they sum to 0.99

so we need a distribution of

$P[k] = \dfrac{1}{0.99} \times \{0.2, 0.2, 0.2, 0.1, 0.1, 0.1, 0.02, 0.02, 0.02, 0.01, 0.01, 0.01\}$

The way to get this is to use a 99 sided die.

$1-20 \to \text{item 1}$
$21-40 \to \text{item 2}$
$41-60 \to \text{item 3}$
$61-70 \to \text{item 4}$
$71-80 \to \text{item 5}$
$81-90 \to \text{item 6}$
$91- 92 \to \text{item 7}$
$93-94 \to \text{item 8}$
$95-96 \to \text{item 9}$
$97 \to \text{item 10}$
$98 \to \text{item 11}$
$99 \to \text{item 12}$

You can adjust this easily to use a 99k sided die but if your RNG is truly random (which of course it isn't) this mapping will work fine.

How you implement this in your game language I leave to you to figure out.

3. ## Re: Generating Probability in a sequence

I don't think my previous reply went through but I believe I figured it out.

My notes look like nonsense: (I know this placeholder syntax is garbage)
ifrand(33) mint
ifrand(61) white
ifrand(77) pink
ifrand(66) blue
else red
else
ifrand(50) jawbreaker
ifrand(61) white
ifrand(77) pink
ifrand(66) blue
else red

else chocolate
ifrand(61) white
ifrand(77) milk
ifrand(66) dark
else pepper

i.e. there's a 33% chance it'll be mint, otherwise it's a jawbreaker or chocolate. If it's a mint, there's a 66% chance it'll be white, if not there's a 77% chance it'll be pink, if not there's a 66% chance it'll be blue, otherwise it'll be red.

But I think what this structure will do is give a roughly 20% chance of getting White Mint, White Jawbreaker, or White Chocolate; 10% Pink Mint, Pink Jawbreaker, or Milk Chocolate; 2% chance of Blue Mint, Blue Jawbreaker, Dark Chocolate; or 1% chance of Red Mint, Red Jawbreaker, Pepper Chocolate.
Yes, all this work is to distribute candy prizes.
It makes sense in the game.