create array of random numbers in swift

前端 未结 5 2260
忘了有多久
忘了有多久 2021-02-14 19:29

I\'m just starting to learn swift. I\'m attempting to create an array of several random numbers, and eventually sort the array. I\'m able to create an array of one random number

5条回答
  •  花落未央
    2021-02-14 20:10

    Ok, this is copy/paste of a question asked elsewhere, but I think I'll try to remember that one-liner :

    var randomArray = map(1...100){_ in arc4random()}
    

    (I love it !)

    EDIT

    If you need a random number with an upperBound (exclusive), use arc4random_uniform(upperBound)

    e.g. : random number between 0 & 99 : arc4random_uniform(100)

    Swift 2 update

    var randomArray = (1...100).map{_ in arc4random()}
    

提交回复
热议问题