Random loot - rarity


I’m trying to work out a system (in javascript) that will spawn an item in a pre-placed loot pile. Using Random.Range, I could have easily just had it so that any item spawns randomly, but I want different items to be rarer/more common than others. If you’re not sure what I’m on about I’m trying to replicate the Day-Z loot system Day-Z wiki.

So far I’ve created an ‘item list’ that just has an array for an item, with it’s name and then it’s object. The variable name is the item’s ‘data value’.

static var i4310 = Array("Riot Shotgun Slugs", Resources.Load("i4310"));

Also I have another script that says how rare it is in certain areas.

//Rarity out of 100

/*i4310 - riot shotgun ammo*/ static var i4310Rarity = 40; //40 for testing purposes

So my question is; How can you make items spawn at pre-placed loot spawn locations, and have a different rarity for each item?

Hopefully that wasn’t too much to ask, thanks in advance!

you can use a Random.Range(0, Rarity), this can be used to determine if it will spawn an item. although the higher the rarity the rarer the item would be.

Use a table of rarity values and ranges for instance item x has a rarity of 5 so it a random generated value is between 0 and 5 spawn that item or use percents aka have a imaginary bar of 0-1000 and divide that by the rarity of an item again lets say item x has a 5% chance of getting discovered then if random number drops in the first 5% of 1000 then generate that loot.