Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Random numbers in Ruby

   1  
   2  
   3  # Pseudo random number generator
   4  # From: http://gnuvince.wordpress.com/2005/11/24/pseudo-random-number-generator
   5  # Author: Vincent Foley-Bourgon
   6  
   7  def random_number
   8    t = Time.now.to_f / (Time.now.to_f % Time.now.to_i)
   9    random_seed = t * 1103515245 + 12345;
  10    (random_seed / 65536) % 32768;
  11  end
  12  
  13  10.times { puts random_number }
  14  
  15  cnt = Array.new(10,0)
  16  20000.times { cnt[(random_number % 10).to_i] += 1 }
  17  p cnt
  18  puts
  19  
  20  
  21  # Ruby Gaussian Random Number Generator
  22  # Author: Glenn
  23  # http://webhost101.net/rails/typo/articles/2007/07/31/ruby-gaussian-random-number-generator
  24  
  25  def gaussian_rand 
  26     u1 = u2 = w = g1 = g2 = 0  # declare
  27     begin
  28       u1 = 2 * rand - 1
  29       u2 = 2 * rand - 1
  30       w = u1 * u1 + u2 * u2
  31     end while w >= 1
  32     
  33     w = Math::sqrt( ( -2 * Math::log(w)) / w )
  34     g2 = u1 * w;
  35     g1 = u2 * w;
  36     # g1 is returned  
  37  end
  38  
  39  sum = 0
  40  sumsq = 0
  41  n = 1000
  42  0.upto(n) do 
  43    #r = gaussian_rand
  44    r = gaussian_rand * 100 + 50   # new_random_number = gaussian_rand * standard_deviation + average
  45    #puts r
  46    sum += r
  47    sumsq += (r*r)
  48  end
  49  
  50  ave = sum/n
  51  stddev = Math::sqrt(sumsq/n - ave * ave)
  52  puts "Average = #{ave}"
  53  puts "StdDev = #{stddev}"
  54  puts
  55  
  56  
  57  # Random Number Generator
  58  # http://scutigena.sourceforge.net/test-random.html
  59  # http://scutigena.sourceforge.net/sources/ruby-1.7.2/random.html
  60  # $Id: random.ruby,v 1.2 2003/12/30 01:25:05 davidw Exp $
  61  
  62  IM = 139968
  63  IA = 3877
  64  IC = 29573
  65  
  66  $last = 42.0
  67  def gen_random (max) (max * ($last = ($last * IA + IC) % IM)) / IM end
  68  
  69  10.times do
  70      puts gen_random(100000.0)
  71  end
  72  
  73  printf "%.5f\n", gen_random(100.0)
  74  puts
  75  
  76  
  77  # From: http://matt.blogs.it/entries/00002641.html
  78  # Author: Matt Mower
  79  # cf. http://www.taygeta.com/random/gaussian.html
  80  # cf. http://www.bearcave.com/misl/misl_tech/wavelets/hurst/random.html
  81  
  82  def box_mueller( mean = 0.0, stddev = 1.0 )
  83    x1 = 0.0, x2 = 0.0, w = 0.0
  84  
  85    until w > 0.0 && w < 1.0
  86      x1 = 2.0 * rand - 1.0
  87      x2 = 2.0 * rand - 1.0
  88      w = ( x1 * x2 ) + ( x2 * x2 )
  89    end
  90  
  91    w = Math.sqrt( -2.0 * Math.log( w ) / w )
  92    r = x1 * w
  93  
  94    mean + r * stddev
  95  end
  96  
  97  10.times { puts box_mueller(5.0, 1.0) }
  98  puts
  99  
 100  
 101  # Generating random numbers with a specified distribution
 102  # From: http://www.cs.nyu.edu/~michaels/blog/?p=24
 103  # Author: Michael Schidlowsky
 104  
 105  def gen
 106    (x=rand)>0.5 ? x : (x < rand/2.0) ? 1.0 - x : x
 107  end
 108  
 109  def gen2
 110     (x = rand) && rand ? 1.0 - x : x
 111  end
 112  
 113  def compute_distribution(numSamples, &block)
 114     samples = []
 115     values = 10
 116     numSamples.times{samples << yield}
 117     dist = Array.new(values, 0)
 118     samples.each{ |s| dist[(s*values).floor] += 1 }
 119     dist
 120  end
 121  
 122  p compute_distribution(1000){rand}
 123  p compute_distribution(1000){gen}
 124  p compute_distribution(1000) { gen2 }
 125  puts
 126  
 127  
 128  # Random number generation for a triangular distribution
 129  # From: http://www.brighton-webs.co.uk/distributions/triangular.asp
 130  # cf. http://en.wikipedia.org/wiki/Triangular_distribution
 131  
 132  def rng(m, low, high)
 133  
 134     # cf. the parameter info at http://www.brighton-webs.co.uk/distributions/triangular.asp
 135     return nil unless high > low && m > low && m < high    
 136  
 137     u = rand
 138  
 139     if u <= (m-low)/(high-low)
 140        r = low+ Math.sqrt(u*(high-low)*(m-low))
 141     else
 142        r = high - Math.sqrt((1.0-u)*(high-low)*(high-m))
 143     end
 144  
 145  end
 146  
 147  10.times do
 148    #puts rng(0.0, -25.0, 25.0)
 149    puts rng(50.0, 25.0, 75.0)
 150  end
 151  puts
 152  
 153  
 154  # From: http://www.ruby-forum.com/topic/95931
 155  # Author: Christoffer Lernö
 156  
 157  class Range
 158    def rand
 159      return first if exclude_end? && last == first
 160      Kernel::rand(last - first + (exclude_end? ? 0 : 1)) + first
 161    end
 162  end
 163  
 164  r1 = -9..9
 165  r2 = -90...100
 166  p r1.rand, r2.rand
 167  
 168  r3 = 0..9
 169  
 170  num = []
 171  10.times do
 172     num << r3.rand
 173     if num.first.zero? then num.shift; redo end
 174  end
 175  
 176  p num
 177  puts num.to_s.to_i
 178  puts
 179  
 180  
 181  # See:
 182  # http://redcorundum.blogspot.com/2008/01/randomizing-array-revisited.html
 183  # http://redcorundum.blogspot.com/2006/12/randomizing-array-and-other-faqs.html
 184  # http://szeryf.wordpress.com/2007/06/19/a-simple-shuffle-that-proved-not-so-simple-after-all/
 185  
 186  class Array
 187    def shuffle
 188      array = dup
 189      size.downto 2 do |j|
 190        r = rand j
 191        array[j-1], array[r] = array[r], array[j-1]
 192      end
 193      array
 194    end
 195  end
 196  
 197  array = (1..50).to_a
 198  
 199  10.times { p array.shuffle.first(10) }
 200  
 201  
 202  #-------------------
 203  
 204  
 205  class RNG
 206  
 207     def num(min=8,max=min+5,iter=1)
 208  
 209        return nil unless min.is_a?(Integer) && max.is_a?(Integer) && max > min && iter.is_a?(Integer)
 210  
 211        ret = []
 212        stats = Hash.new(0)     # optional; cf. stats[random_num] += 1
 213        digits = Array(0..9).sort_by {rand}
 214        #digits = (Array(0..9) * (rand(4)+1)).sort_by {rand}
 215        digits_size = digits.size
 216  
 217        iter.times do
 218           count = 0
 219           len = min + rand(max-min+1)
 220           ar = []
 221           while count < len
 222              i = rand(digits_size)    # get a random array index
 223              random_num = digits.at(i)
 224              stats[random_num] += 1
 225              ar << random_num
 226              digits = digits.sort_by {rand}
 227              count += 1
 228              if count == 1 && ar.first.zero? then ar.shift; stats[0] -= 1; count = 0 end
 229           end
 230           ret << ar.to_s.to_i
 231        end  # iter
 232        #ret
 233        ret << stats   # optional
 234     end
 235  
 236  end
 237  
 238  puts
 239  
 240  min = 8
 241  max = 13
 242  iter = 10000
 243  result =  RNG.new.num(min, max, iter)
 244  stats = result.pop
 245  
 246  t = 0
 247  stats.each_value { |v| t += v }  # get the overall frequency
 248  
 249  n = 0
 250  t = t * 1.0
 251  m = t / 10.0 
 252  
 253  stats.sort.each do |k,v| 
 254     i = (v / t) * 100.0
 255     x = v > m ? v - m : m - v
 256     y = (x / m) * 100.0
 257     n += i
 258     puts "#{k}  ::  #{"%.2f" % m}  ::  #{v}  ::  #{"%.2f" % i} %  ::  #{ "%.2f" % ((m-v) * -1) }  ::  #{"%.2f" % y} %"
 259  end
 260  
 261  puts n, m, t
 262  
 263  puts
 264  puts RNG.new.num(20, 25, 10)[0..-2]
 265  
 266  puts
 267  rn = RNG.new.num(80, 100)
 268  puts rn[0..-2]
 269  p rn.last
 270  

Shuffling an Array in Ruby

// creates a new method on Array called shuffle!

   1  
   2  class Array
   3    def shuffle!
   4      size.downto(1) { |n| push delete_at(rand(n)) }
   5      self
   6    end
   7  end
   8  
   9  a = [1,2,3,4,5,6,7,8,9]
  10  a.shuffle!
  11  => [5, 2, 8, 7, 3, 1, 6, 4, 9]

Using random module

   1  
   2  >>> import random
   3  
   4  >>> # 0.0 <= float < 1.0
   5  >>> random.random()
   6  0.41360177662769904
   7  
   8  >>> # 10.0 <= float < 20.0
   9  >>> random.uniform(10,20)
  10  15.743669918803288
  11  
  12  >>> # 10 <= int <= 20  (can be 20)
  13  >>> random.randint(10,20)
  14  10
  15  
  16  >>> # 10 <= int < 20  (even only, coz step=2)
  17  >>> random.randrange(10,20,2)
  18  16
  19  
  20  >>> # choose from a list
  21  >>> random.choice([1, 2, 3, 5, 9])
  22  2
  23  
  24  >>> # make a list into random order
  25  >>> cards = range(52)
  26  >>> random.shuffle(cards)  # order is random now
  27  >>> cards[:5]   # get 5 cards
  28  [37, 14, 42, 44, 6]

Array Shuffle //JavaScript Function



[UPDATED CODE AND HELP CAN BE FOUND HERE]


usage

   1  
   2  alert(shuffle([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));


code based on Fisher-Yates (never heard about him hahaha) algorithm

   1  
   2  //+ Jonas Raoni Soares Silva
   3  //@ http://jsfromhell.com/array/shuffle [v1.0]
   4  
   5  shuffle = function(o){ //v1.0
   6  	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
   7  	return o;
   8  };
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS