How do I shuffle an array in Swift?

前端 未结 25 2184
长发绾君心
长发绾君心 2020-11-21 05:44

How do I randomize or shuffle the elements within an array in Swift? For example, if my array consists of 52 playing cards, I want to shuffle the array in o

25条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-21 06:33

    You can use generic swap function as well and implement mentioned Fisher-Yates:

    for idx in 0..

    or less verbose:

    for idx in 0..

提交回复
热议问题