Ruby中的随机数
我试图使n个随机数从-0.5到0.5 我做了一个这样的函数Ruby中的随机数,ruby,noise,Ruby,Noise,我试图使n个随机数从-0.5到0.5 我做了一个这样的函数 def create_noise(n) end 我找到了一个实现,但我认为它不起作用 randoms = Set.new loop randoms << rand(max) return randoms.to_a if randoms.size >= n randoms=Set.new 环 随机数=n 你就这么做了 def create_noise(n) n.ti
def create_noise(n)
end
我找到了一个实现,但我认为它不起作用
randoms = Set.new
loop
randoms << rand(max)
return randoms.to_a if randoms.size >= n
randoms=Set.new
环
随机数=n
你就这么做了
def create_noise(n)
n.times.collect { rand(-0.5..0.5) }
end
这将返回一个如下所示的数组:
[-0.034680737617880486, 0.34802029078157803, 0.1346483808607455, 0.12155616615186282, -0.41043213731234474]