Search code examples
rubynoise

Random numbers in Ruby


I am trying to make n random numbers from -0.5 from to 0.5

and I made a function like this

def create_noise(n)

end

I found an implementation of this but i don't think this works

 randoms = Set.new 
      loop 
       randoms << rand(max)
       return randoms.to_a if randoms.size >= n

Solution

  • You would just do

    def create_noise(n)
      n.times.collect { rand(-0.5..0.5) }
    end
    

    that will spit back an array like this:

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