- Home /

# X in X chance to roll a drop table.

So essentially what I am trying to do is be able to set a value that will determine the chance that you will receive an item that is dropped. For example you open a chest, when you open that chest it drops a few items. I'm trying to figure out how to always get a drop, but based on chance. So let's say it drops a gold coin, a sword, and a helmet. I want the gold coin to be more common with a 1/10 chance to be rolled. The sword to have a 1/128 chance to be rolled, and the helmet to also be a 1/128 chance to be rolled. I also want there to be a drop every single time. So that no matter what, you will get one of the three, but the coin is just the most common. How would one going about doing this? I know of `Random.Value && Random.Range`

But im not thinking that either of those are a good way of achieving my goal here. Any suggestions?

**Answer** by toddisarockstar
·
6 days ago

really you could do what you are saying by looping your rolls untill you got a winning roll between the items but the problem with this approach your are mentioning with actually rolling a dice for the odds is "what item do you start with ?" cause if you started "rolling" with something with high odds it would be unfair to the other items chances of showing up and not give accurate results. so another more proficient and truly accurate approach..........

in reality 1 out of 128 can be simplifyed by deviding the 1 by 128 and the result becomes normalized like a percentage. so 1 out of 128 is better for comparison as .0078. one out of ten becomes .1 and so on.

the only difference between these numbers and a percentage is they go from zero to one instead of one to a hundred.

once you have all your normalized "percentage" numbers. you can add them all together, pick a random range to the total and see where you fall.

```
float item0 = 1f/20;
float item1 = 1f/128;
float item2 = 1f/36;
float item3 = 4f/121;
// real percentages
print ("item0 odds of winning "+(item0*100)+"%");
print ("item1 odds of winning "+(item1*100)+"%");
print ("item2 odds of winning "+(item2*100)+"%");
print ("item3 odds of winning "+(item3*100)+"%");
// put our numbers in an array for easy reading
float[] allitems = new float[]{item0,item1,item2,item3};
float total = 0;
//add all the items percentages together
int i = allitems.Length;
while(i>0){i--;total+=allitems[i];}
float roll = Random.Range (0f, total);
print ("total of all percentages is:" + total+ " and my random is:"+roll);
i = 0;
int answer = 0;
float c = 0;
//see what increment the random falls into
while(i<allitems.Length){
c+=allitems[i];
if(roll<c){answer=i;break;}
i++;}
print ("I give you item number:" + answer+" ... have a nice day");
```

I see. I like this. I'm gonna play around with concept a bit and see what I can come up with. I'll post back with results once I get started.

Just wanted to add that System.Random allways gives the same numbers with the same seed. You might want to use it for testing.

**Answer** by CKong91
·
6 days ago

If you want there to be a drop every single time, and gold coin to be more common, why not just set gold cold as the default drop when you can't rolled other item? Other than that, you can use Random.Range(0,1000) and if the result value is smaller than 128, drop item will be sword/helmet, then you can use another Random to determine what the drop will be if there are more than 2 drop with same rolled value. Another advice is that you should organize and put these drop value into a text based file/ excel for ease of edit/ modify.

I intend to pull the items from an external file just for organization and safe keeping. I don't want any default drops though, I want everything to be based on a X in Y chance. Total rng. Which could prove a test in the end lol

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.