How to compare random numbers in Swift -


i’m beginner in programming , playing around arc4random_uniform() function in swift. program i’m making far generates random number 1-10 regenerated uibutton. however, want variable ’highest' gets initialised random number update if next generated number larger 1 held in it. example random number 6 stored in highest , if next number 8 highest becomes 8. don't know how go this. have connected uibutton ibaction function , have following code:

    var randomvalue = arc4random_uniform(11) + 1     highest = int(randomvalue)      if (int(randomvalue) < highest) {         // don’t know     } 

initialise highest 0

every time generate new random number, replace value of highest higher of 2 numbers

highest = max(highest, randomvalue) 

the max() function part of swift standard library , returns larger of 2 passed in vales.

edited add

here's playground showing bit more detail, including casting of types:

var highest: int = 0  func random() -> int {     let r = arc4random_uniform(10) + 1     return int(r) }  var randomvalue = random() highest = max(highest, randomvalue) 

you can see multiple calls persist highest value.

enter image description here


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -