How do I create a list of random numbers without duplicates?

前端 未结 17 2447
灰色年华
灰色年华 2020-11-22 13:30

I tried using random.randint(0, 100), but some numbers were the same. Is there a method/module to create a list unique random numbers?

Note: The fol

17条回答
  •  旧巷少年郎
    2020-11-22 13:33

    If the list of N numbers from 1 to N is randomly generated, then yes, there is a possibility that some numbers may be repeated.

    If you want a list of numbers from 1 to N in a random order, fill an array with integers from 1 to N, and then use a Fisher-Yates shuffle or Python's random.shuffle().

提交回复
热议问题