iOS: How do I generate 8 unique random integers?

前端 未结 6 1006
天命终不由人
天命终不由人 2020-12-03 15:10

I need to generate 8 random integers, but they need to be unique, aka not repeated.

For example, I want 8 numbers within the range 1 to 8.

I\'ve seen arc4ran

6条回答
  •  既然无缘
    2020-12-03 15:47

    Here's some psuedo code

    1. For each number 1-8 generate a random number.
    2. Add the random number and the integer to a dictionary as a key value pair
    3. get all the keys of the dictionary as an array (hint: have a look at the allKeys method)
    4. Sort this array (ascending or descending isn't important)
    5. Now for each of these numbers as the key, get the corresponding integer from the dictionary

提交回复
热议问题