1

I recentrly started to work with swift, and me and my Mate are trying to make a Small game to test what we learned.. we made a sort of monster fighting game ,no moving object just clicking on the monster. So far so good evrything works as it should, got Upgrade to get more Hp or Str to do more damage on the monster, and to get more Hp when it hits back. that said. i'm made items to the game but i want them to drop by the monster. made different types and would like to know if anyone knows a code to get a Drop Chance on the Items... like

"Normal" = 73%
"Rare" = 25%
"Legend" = 2%

I Looked around on evry search engine but can't find anything that i need. Hope to get a reply soon Thanks. Greetz Kristof.V

Kristof.V
  • 143
  • 1
  • 7

3 Answers3

2

This logic is the same of the answer by Avt, just a different implementation.

let random = Int(arc4random_uniform(100))

switch random {
case 0...72: print("Normal")
case 73...97: print("Rare")
case 98...99: print("Legend")
default: fatalError("Wrong number")
}
Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
1

Use a random function arc4random_uniform

Returns a random number between 0 and the inserted parameter minus 1.

For example arc4random_uniform(3) may return 0, 1 or 2 but not 3.

You can use it like:

let random = Int(arc4random_uniform(100))

if random < 73 { 
     // drop normal
}
else if random < 73+25 { 
     // drop rare
} else {
     // drop legend
}
Community
  • 1
  • 1
Avt
  • 16,927
  • 4
  • 52
  • 72
1

(Edited: to include also weapon type as per follow-up question Random number to print array (Swift))

As above, use the arc4random function. A more complete "Monster" example follows.


It could be appropriate to hold different rarity and weapon types as enum cases

enum Rarities {
    case Normal
    case Rare
    case Legendary
}

enum Weapons {
    case Knife
    case Sword
    case Katana
}

Each monster could be an instance of a Monster class with it's own specific item and rarity drop rates, initialized when creating the Monster object.

class Monster {
    var name : String
    var dropRatesRarity = [Rarities:Double]()
    var dropRatesWeapons = [Weapons:Double]()

    init(name: String, dropRatesRarity: [Rarities:Double], dropRatesWeapons: [Weapons:Double]) {
        self.name = name
        var rateSum = dropRatesRarity.values.reduce(0.0, combine: +)
        var dropRatesCumSum = 0.0
        for (k, v) in dropRatesRarity {
            self.dropRatesRarity[k] = v/rateSum + dropRatesCumSum
            dropRatesCumSum += v/rateSum
        }
        rateSum = dropRatesWeapons.values.reduce(0.0, combine: +)
        dropRatesCumSum = 0.0
        for (k, v) in dropRatesWeapons {
            self.dropRatesWeapons[k] = v/rateSum + dropRatesCumSum
            dropRatesCumSum += v/rateSum
        }
    }

    func dropItem() -> (Weapons,Rarities) {
        return (generateItemType(), generateRarity())
    }

    func generateRarity() -> Rarities {
        let random = Double(Float(arc4random()) / Float(UINT32_MAX))
        return dropRatesRarity.filter({ (k, v) in v >= random }).minElement({ $0.1 < $1.1 })?.0 ?? .Normal
    }

    func generateItemType() -> Weapons {
        let random = Double(Float(arc4random()) / Float(UINT32_MAX))
        return dropRatesWeapons.filter({ (k, v) in v >= random }).minElement({ $0.1 < $1.1 })?.0 ?? .Knife
    }
}

Example for some Monster instance:

/* Example */
var myDropRatesRarity = [Rarities:Double]()
myDropRatesRarity[.Normal] = 73 // relative drop rates, needn't sum to 100
myDropRatesRarity[.Rare] = 25
myDropRatesRarity[.Legendary] = 2

var myDropRatesWeapons = [Weapons:Double]()
myDropRatesWeapons[.Knife] = 50
myDropRatesWeapons[.Sword] = 30
myDropRatesWeapons[.Katana] = 20

var myMonster = Monster(name: "Godzilla", dropRatesRarity: myDropRatesRarity, dropRatesWeapons: myDropRatesWeapons)

var myItem = myMonster.dropItem()
print(myMonster.name + " dropped a \(myItem.0) of \(myItem.1) rarity!")
/* "Godzilla dropped a Katana of Normal rarity!" */
/* ... most likely Normal rarity ... keep grinding! */
Community
  • 1
  • 1
dfrib
  • 70,367
  • 12
  • 127
  • 192