Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby中的随机数_Ruby_Noise - Fatal编程技术网

Ruby中的随机数

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

我试图使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.times.collect { rand(-0.5..0.5) }
end
这将返回一个如下所示的数组:

[-0.034680737617880486, 0.34802029078157803, 0.1346483808607455, 0.12155616615186282, -0.41043213731234474]