Advertisement
cd62131

Normal Random 0 .. 100

Dec 5th, 2013
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.63 KB | None | 0 0
  1. #!/usr/bin/ruby
  2. # coding: utf-8
  3. def normalrandom1
  4.   12.times.map{ Random.rand }.inject(:+) - 6
  5. end
  6.  
  7. @switch = true
  8. @t = nil
  9. @s = nil
  10. def normalrandom2
  11.   if @switch
  12.     @switch = false
  13.     @t = Math.sqrt(-2 * Math.log(1 - Random.rand))
  14.     @u = 2 * Math::PI * Random.rand
  15.     @t * Math.cos(@u)
  16.   else
  17.     @switch = true
  18.     @t * Math.sin(@u)
  19.   end
  20. end
  21.  
  22. 100.times do
  23.   a = normalrandom1 * 50 / 2 + 50
  24.   a = (a < 0) ?  0 : a
  25.   a = (a > 100) ? 100 : a
  26.   print "#{a.floor} "
  27. end
  28. puts
  29. 100.times do
  30.   a = normalrandom2 * 50 / 2 + 50
  31.   a = (a < 0) ?  0 : a
  32.   a = (a > 100) ? 100 : a
  33.   print "#{a.floor} "
  34. end
  35. puts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement