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?

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.

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*;}*

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*;_
_
if(roll<c){answer=i;break;}_
_
i++;}*_

* print (“I give you item number:” + answer+" … have a nice day");*