Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift probability of random number being selected?

Tags:

swift

I am trying to make a game where you can upgrade a specific item of yours. The quality of the new item is going to be random and based on something like this:

10% worse - 35% better then current item level = 55% chance
36% better to 90% better then current item level = 35% chance
91% better to 200% better then current item level = 5% chance
201% better to 500% better then current item level = 2.5% chance
500% better to 2000% better then current item level = 2.5% chance

How would I go about making it so a random number that is generated would have a, for example, 55% chance of being only 35% better? I had a crack and came up with this (ignore the % numbers, I was just using this one for testing).

let randomNumber = Int(arc4random_uniform(1000))


if randomNumber <= 700 {
    println("hey")
    var newLevel = (Double(Double(randomNumber) / 700.00) + 0.1) * Double(pickDamage)

} else if randomNumber <= 800 && randomNumber > 700 {
    var newLevel = (Double(Double(randomNumber) / 700.00) + 1.00) * Double(pickDamage)
} else if randomNumber <= 1000 && randomNumber > 950 {
    var newLevel = (Double(Double(randomNumber) / 700.00) + 2.00) * Double(pickDamage)
}

But this isn't really doing it the way I want.

like image 806
Harazzy Avatar asked Nov 26 '25 22:11

Harazzy


1 Answers

Why not use the Swift switch statement combined with pattern matching:

// create a random percent, with a precision of one decimal place
func randomPercent() -> Double {
  return Double(arc4random() % 1000) / 10.0;
}

let randomNumber = randomPercent()
switch(randomNumber) {
case 0..<55:
  println("10% worse - 35% better then current item level")
case 55..<90:
  println("36% better to 90% better then current item level")
case 90..<95:
  println("91% better to 200% better then current item level")
case 95..<97.5:
  println("201% better to 500% better then current item level")
default:
  println("500% better to 2000% better then current item level")
}

This makes the logic very clear.

like image 105
ColinE Avatar answered Nov 29 '25 14:11

ColinE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!