create array of random numbers in swift

前端 未结 5 2076
面向向阳花
面向向阳花 2021-02-14 19:19

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:16

    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()}
    

提交回复
热议问题