· 1 min read
Eine Liste mit Zufallszahlen erzeugen
Beispiel 1:
[ruby]
n.times.inject([]) {|r,n| v = rand(max) until v and not r.include? v; r << v}
#=> [1, 0, 7, 6, 3]
[/ruby]
Beispiel 2:
[ruby]
(1..5).inject([]) do |r,n|
v = rand(max)
redo if r.include? v
r << v
end
[/ruby]
Beispiel 3:
[ruby]
5.times.inject([]) {|r,n| v = rand(max) while v.nil? or r.include? v; r << v}
[/ruby]