How to generate non repeating random number

前端 未结 4 977
甜味超标
甜味超标 2021-01-07 04:45

I am trying to randomize numbers in an array. I am able to do that using arc4random() % [indexes count]

My problem is - If an array consists of 20 item

4条回答
  •  天命终不由人
    2021-01-07 05:30

    The problem with all these answers is that you need to review your previous generated random numbers and that takes extra time if you need a large number of random integers.

    Another solution is using cryptography:

    1. Generate a random key
    2. Iterate between 0..n
    3. Encrypt each integer and apply modulo the number of alternatives do you want to use to the output of the function.

    There are some extra details to take into account that don't matter for your case.

提交回复
热议问题