How do I generate a list of n unique random numbers in Ruby?

后端 未结 15 1835
鱼传尺愫
鱼传尺愫 2020-11-28 22:27

This is what I have so far:

myArray.map!{ rand(max) }

Obviously, however, sometimes the numbers in the list are not unique. How can I mak

15条回答
  •  难免孤独
    2020-11-28 23:02

    How about a play on this? Unique random numbers without needing to use Set or Hash.

    x = 0
    (1..100).map{|iter| x += rand(100)}.shuffle
    

提交回复
热议问题