# Completing a chance system with relative probabilites

Status
Not open for further replies.

#### Smiddle

Level 8
Yo. I want to spawn items regularly, and there are different classes of items, each class having a relative probability. Within each class, there's also a number of items, each with its relative probability within.

Each class is characterised by an integer, with the rel.prob. stored in an integer array. Within each class, the items are handled the same way. I have no problem with converting relative probabilities into absolute probabilities - you just divide each prob with the sum of all probs.

However, I don't really know how to take the final step, i.e. to actually pick a random item given the probability for each item.

How can I complete the system? Maybe it's not a good way to programme the system to begin with?

#### Wietlol

Level 24
Create a real between 0 and the chances of all items together.
Then loop through the list of items and lower that created real by the chance of the picked item.
as soon as that real becomes equal to or less than 0, the picked item is the one that has to spawn

#### Dr Super Good

Spell Reviewer
Level 63
In theory you can use a binary tree to determine which item to spawn based on the sum of probabilities. This scales very well due to the logarithmic complexity of binary trees.

Status
Not open for further replies.

Replies
5
Views
621
Replies
18
Views
2K
Replies
6
Views
344
S
Replies
7
Views
939
sentrywiz
S
Replies
6
Views
637