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-10 of 42 total  RSS 

Bash title random Quote routine

#
#this is a script in my .bash_profile that gets a random
#quote .txt file and assigns it to the terminal title.
#

quote_path="/Users/glynch/Documents/quotes/*"
files=($quote_path)
N=${#files[@]}
((N=RANDOM%N))
randomfile=${files[$N]}
quote=""
while read line; do quote="$quote $line"; done < $randomfile
echo -n -e "\033]0;$quote\007"

ruby: random alphanumeric string

generate random alphanumeric string 'size' characters long

size = 8; (1..size).map{([*('a'..'z')]+[*('A'..'Z')]+[*(1..9)].map{|n|n.to_s}).instance_eval{self[rand(self.size)]}}.join

Random numbers in Ruby


# Pseudo random number generator
# From: http://gnuvince.wordpress.com/2005/11/24/pseudo-random-number-generator
# Author: Vincent Foley-Bourgon

def random_number
  t = Time.now.to_f / (Time.now.to_f % Time.now.to_i)
  random_seed = t * 1103515245 + 12345;
  (random_seed / 65536) % 32768;
end

10.times { puts random_number }

cnt = Array.new(10,0)
20000.times { cnt[(random_number % 10).to_i] += 1 }
p cnt
puts


# Ruby Gaussian Random Number Generator
# Author: Glenn
# http://webhost101.net/rails/typo/articles/2007/07/31/ruby-gaussian-random-number-generator

def gaussian_rand 
   u1 = u2 = w = g1 = g2 = 0  # declare
   begin
     u1 = 2 * rand - 1
     u2 = 2 * rand - 1
     w = u1 * u1 + u2 * u2
   end while w >= 1
   
   w = Math::sqrt( ( -2 * Math::log(w)) / w )
   g2 = u1 * w;
   g1 = u2 * w;
   # g1 is returned  
end

sum = 0
sumsq = 0
n = 1000
0.upto(n) do 
  #r = gaussian_rand
  r = gaussian_rand * 100 + 50   # new_random_number = gaussian_rand * standard_deviation + average
  #puts r
  sum += r
  sumsq += (r*r)
end

ave = sum/n
stddev = Math::sqrt(sumsq/n - ave * ave)
puts "Average = #{ave}"
puts "StdDev = #{stddev}"
puts


# Random Number Generator
# http://scutigena.sourceforge.net/test-random.html
# http://scutigena.sourceforge.net/sources/ruby-1.7.2/random.html
# $Id: random.ruby,v 1.2 2003/12/30 01:25:05 davidw Exp $

IM = 139968
IA = 3877
IC = 29573

$last = 42.0
def gen_random (max) (max * ($last = ($last * IA + IC) % IM)) / IM end

10.times do
    puts gen_random(100000.0)
end

printf "%.5f\n", gen_random(100.0)
puts


# From: http://matt.blogs.it/entries/00002641.html
# Author: Matt Mower
# cf. http://www.taygeta.com/random/gaussian.html
# cf. http://www.bearcave.com/misl/misl_tech/wavelets/hurst/random.html

def box_mueller( mean = 0.0, stddev = 1.0 )
  x1 = 0.0, x2 = 0.0, w = 0.0

  until w > 0.0 && w < 1.0
    x1 = 2.0 * rand - 1.0
    x2 = 2.0 * rand - 1.0
    w = ( x1 * x2 ) + ( x2 * x2 )
  end

  w = Math.sqrt( -2.0 * Math.log( w ) / w )
  r = x1 * w

  mean + r * stddev
end

10.times { puts box_mueller(5.0, 1.0) }
puts


# Generating random numbers with a specified distribution
# From: http://www.cs.nyu.edu/~michaels/blog/?p=24
# Author: Michael Schidlowsky

def gen
  (x=rand)>0.5 ? x : (x < rand/2.0) ? 1.0 - x : x
end

def gen2
   (x = rand) && rand ? 1.0 - x : x
end

def compute_distribution(numSamples, &block)
   samples = []
   values = 10
   numSamples.times{samples << yield}
   dist = Array.new(values, 0)
   samples.each{ |s| dist[(s*values).floor] += 1 }
   dist
end

p compute_distribution(1000){rand}
p compute_distribution(1000){gen}
p compute_distribution(1000) { gen2 }
puts


# Random number generation for a triangular distribution
# From: http://www.brighton-webs.co.uk/distributions/triangular.asp
# cf. http://en.wikipedia.org/wiki/Triangular_distribution

def rng(m, low, high)

   # cf. the parameter info at http://www.brighton-webs.co.uk/distributions/triangular.asp
   return nil unless high > low && m > low && m < high    

   u = rand

   if u <= (m-low)/(high-low)
      r = low+ Math.sqrt(u*(high-low)*(m-low))
   else
      r = high - Math.sqrt((1.0-u)*(high-low)*(high-m))
   end

end

10.times do
  #puts rng(0.0, -25.0, 25.0)
  puts rng(50.0, 25.0, 75.0)
end
puts


# From: http://www.ruby-forum.com/topic/95931
# Author: Christoffer Lernö

class Range
  def rand
    return first if exclude_end? && last == first
    Kernel::rand(last - first + (exclude_end? ? 0 : 1)) + first
  end
end

r1 = -9..9
r2 = -90...100
p r1.rand, r2.rand

r3 = 0..9

num = []
10.times do
   num << r3.rand
   if num.first.zero? then num.shift; redo end
end

p num
puts num.to_s.to_i
puts


# See:
# http://redcorundum.blogspot.com/2008/01/randomizing-array-revisited.html
# http://redcorundum.blogspot.com/2006/12/randomizing-array-and-other-faqs.html
# http://szeryf.wordpress.com/2007/06/19/a-simple-shuffle-that-proved-not-so-simple-after-all/

class Array
  def shuffle
    array = dup
    size.downto 2 do |j|
      r = rand j
      array[j-1], array[r] = array[r], array[j-1]
    end
    array
  end
end

array = (1..50).to_a

10.times { p array.shuffle.first(10) }


#-------------------


class RNG

   def num(min=8,max=min+5,iter=1)

      return nil unless min.is_a?(Integer) && max.is_a?(Integer) && max > min && iter.is_a?(Integer)

      ret = []
      stats = Hash.new(0)     # optional; cf. stats[random_num] += 1
      digits = Array(0..9).sort_by {rand}
      #digits = (Array(0..9) * (rand(4)+1)).sort_by {rand}
      digits_size = digits.size

      iter.times do
         count = 0
         len = min + rand(max-min+1)
         ar = []
         while count < len
            i = rand(digits_size)    # get a random array index
            random_num = digits.at(i)
            stats[random_num] += 1
            ar << random_num
            digits = digits.sort_by {rand}
            count += 1
            if count == 1 && ar.first.zero? then ar.shift; stats[0] -= 1; count = 0 end
         end
         ret << ar.to_s.to_i
      end  # iter
      #ret
      ret << stats   # optional
   end

end

puts

min = 8
max = 13
iter = 10000
result =  RNG.new.num(min, max, iter)
stats = result.pop

t = 0
stats.each_value { |v| t += v }  # get the overall frequency

n = 0
t = t * 1.0
m = t / 10.0 

stats.sort.each do |k,v| 
   i = (v / t) * 100.0
   x = v > m ? v - m : m - v
   y = (x / m) * 100.0
   n += i
   puts "#{k}  ::  #{"%.2f" % m}  ::  #{v}  ::  #{"%.2f" % i} %  ::  #{ "%.2f" % ((m-v) * -1) }  ::  #{"%.2f" % y} %"
end

puts n, m, t

puts
puts RNG.new.num(20, 25, 10)[0..-2]

puts
rn = RNG.new.num(80, 100)
puts rn[0..-2]
p rn.last


PHP : Filas al azar / Random Rows

Filas al azar / Random Rows

function dame_unas_filas_al_azar($tabla,$numero_de_filas)
{
$sql="SELECT * FROM ".$tabla." ORDER BY RAND(NOW()) LIMIT ".$numero_de_filas;
$resultado = mysql_query($sql);
	if($resultado)
	{
	return mysql_fetch_array($resultado);
	}
	else
	{
	return false;
	}
}

Lottery


Author: ntk
License: The MIT License, Copyright (c) 2007 ntk
Inspired by: Lottery Quick Pick
Hints: Lottery mathematics


class Lottery

   def playing(i=1, n1=0, r1=0..0, n2=0, r2=0..0)

      return nil unless i.is_a?(Integer) && n1.is_a?(Integer) && n1 > 0 && r1.is_a?(Range) && n2.is_a?(Integer) && r2.is_a?(Range)
      stats = Hash.new(0)   # optional; stores the frequency of picked numbers, cf. stats[random_num] += 1
      ret = []

      random_procs = [ 
         lambda { |a,n| a.slice!(rand(n)) },      # a is an array, n an integer
         lambda { |a,n| a.slice!(rand(n) * -1) }, 
         lambda { |a,n| a.slice!((rand(n) * -1) + rand(n)) },
         lambda { |a,n| a.slice!((rand(n) - rand(n))) }, 
         lambda { |a,n| a.reverse!.slice!(rand(n)) },
         lambda { |a,n| a.reverse!.slice!(rand(n) * -1) }, 
         lambda { |a,n| a.reverse!.slice!((rand(n) * -1) + rand(n)) },
         lambda { |a,n| a.reverse!.slice!((rand(n) - rand(n))) } 
      ] 

      i.times do
         numbers = r1.to_a.sort_by {rand}
         num = numbers.size
         ar = []
         count = 0

         while count < n1 + n2

            count += 1

            if count > n1 && n2 > 0
               numbers2 = r2.to_a
               numbers2 = (numbers & numbers2).sort_by {rand} 
               num2 = numbers2.size
               count2 = 0
               while count2 < n2
                  count2 += 1
                  i = rand(random_procs.size)   # get a random array index for random_procs array
                  random_procs = random_procs.sort_by {rand}      
                  random_num = random_procs.at(i).call(numbers2, num2)
                  stats[random_num] += 1
                  ar << random_num
                  numbers2 = numbers2.sort_by {rand}
                  num2 -= 1
               end   # while

               if count2 > 1 then count += (count2-1) end  
               #break   # alternative
              
            else

               i = rand(random_procs.size)                               
               random_procs = random_procs.sort_by {rand}  
  
               random_num = random_procs.at(i).call(numbers, num)
               stats[random_num] += 1
               ar << random_num

=begin
              if rand(num) % (rand(num) + 1) == rand(num)               #  alternative
                  random_num = random_procs.at(i).call(numbers, num)
                  stats[random_num] += 1
                  ar << random_num
               else
                  random_num = numbers.slice!(rand(num))
                  stats[random_num] += 1
                  ar << random_num
               end
=end

               numbers = numbers.sort_by {rand}
               num -= 1

            end          # if
         end             # while
         ret << ar
      end
      #ret
      ret << stats   # optional
   end

end


puts
puts "UK National Lottery, German Lotto 6/49, ..."

ndraws = 10
result = Lottery.new.playing(ndraws, 6, 1..49)
stats = result.pop

result.each do |t| 
   puts t.sort.join('-')
end

puts
n = 0
stats.sort.each do |k,v| 
   i = (v / (ndraws * 6.0) ) * 100.0
   n += i
   puts "#{k} :: #{v} :: #{i}%"
end
p n

puts
puts "US Powerball Lottery"
Lottery.new.playing(ndraws, 5, 1..55, 1, 1..42)[0..-2].each do |t| 
   n = t.pop
   puts "#{t.sort.join('-')}  and  #{n}"
end

puts
puts "Mega Millions"
Lottery.new.playing(ndraws, 5, 1..56, 1, 1..46)[0..-2].each do |t| 
   n = t.pop
   puts "#{t.sort.join('-')}  and  #{n}"
end

puts
puts "EuroMillions"
Lottery.new.playing(ndraws, 5, 1..50, 2, 1..9)[0..-2].each do |t| 
   n1 = t.pop
   n2 = t.pop
   puts "#{t.sort.join('-')}  and  #{n1}-#{n2}"
end


puts
puts "US Powerball Lottery"

ndraws = 10
nums = [5, 23, 25, 33, 34]
pb = 31

matches = []
n = 0

Lottery.new.playing(ndraws, 5, 1..55, 1, 1..42)[0..-2].each do |t| 
   i = t.pop
   t = t.sort
   if (t & nums).size > matches.size || ( matches.size == 0 && i == pb )
      matches = t & nums 
      i == pb ? n = i : n = 0
   end
end

if pb == n && !matches.empty?
   puts "Your numbers: #{nums.join('-')} and #{pb}\nYour matches: #{matches.join('-')} and #{n}"
elsif pb == n && matches.empty?
   puts "Your numbers: #{nums.join('-')} and #{pb}\nYour matches: [] and #{n}"
else
   puts "Your numbers: #{nums.join('-')} and #{pb}\nYour matches: #{matches.join('-')}"
end


Random Characters with Ruby

Use for a password or salt or whatever...

(0..25).inject('') { |r, i| r << rand(93) + 33 }

randomizing an array in ruby (the right way)

Snagged from http://www.rubyquiz.com/quiz113.html

# be sure to use sort_by rather than sort
quiz = (1..10).to_a
quiz.sort_by { rand }

Api key (or any kind of key) generator

Generates a random key for API neatness.

class KeyGenerator
  require "digest/sha1"
  def self.generate(length = 10)
    Digest::SHA1.hexdigest(Time.now.to_s + rand(12341234).to_s)[1..length]
  end
end

Weighted Random array selection

Say you have an array of ['apples', 'oranges', 'bananas', 'kiwis']

Selecting a random item is simple enough
  arr = ['apples', 'oranges', 'bananas', 'kiwis']
  arr[rand(arr.size)]


But say you want to have apples come up 50% of the time, oranges 25%, bananas 15% and kiwis 10% ...


  class Array
    
    #sum (and mean) found on http://snippets.dzone.com/posts/show/2161
    def sum
      inject( nil ) { |sum,x| sum ? sum+x : x }
    end
    
    def probability(spread = 2)
      z = 1.0
      collect {|x| z = z / spread}
    end
    def weighted_random_index(probability_array = probability(2) )
      size.times do |x|
        return x if rand < probability_array[0..x].sum
      end
      return 0
    end
    
    def get_weighted_random_item(probability_array = probability(2))
      self[weighted_random_index(probability_array)]
    end
    
    def get_weighted_random_indexes(num_items, p = probability(2))
      res = []
      escape = 1000
      while res.size < num_items and escape > 0
        escape -= 1
        tmp = weighted_random_index(p) 
        res << tmp unless res.include?(tmp)
      end
      return res.sort
    end
  
  end


arr = ['apples', 'oranges', 'bananas', 'kiwis']
10.times do |i|
  puts arr.get_weighted_random_item([0.5, 0.25, 0.15, 0.10])
end

oranges
oranges
apples
apples
bananas
apples
apples
bananas
oranges
apples



I'm sure there's some math term for this but I'm calling it 'spread' until I learn otherwise. I put in a default probability array of p = p / 2 which gives you (for an array of 4)

arr.probability
=> [0.5, 0.25, 0.125, 0.0625]


On larger arrays the probabilities will get (for all practicle purposes) zero quickly

arr = [1,2,3,4,5,6,7,8,9]
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
arr.probability
=> [0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625, 0.0078125, 0.00390625, 0.001953125]


I put in a simple spread concept to spread out the probabilities a bit more... the higher the spread, the more even the probability distribution (this varies a lot with array size - but here's a simple example):

   arr = ['apples', 'oranges', 'bananas', 'guava']
   iterations = 10000
   4.times do |t|
     p = arr.probability(t+2)
     res = Array.new(arr.size).fill(0)
     iterations.times do |x|
       res[arr.weighted_random_index(p)] += 1
     end
     res2 = res.collect {|x| (x/iterations.to_f) * 100}
     puts "For probability spread #{t+2}"
     puts "Results #{res2.join(", ")} = #{res2.sum}"
     puts ""
   end

   For probability spread 2
   Results 49.71, 37.55, 11.35, 1.39 = 100.0

   For probability spread 3
   Results 43.44, 29.99, 17.79, 8.78 = 100.0

   For probability spread 4
   Results 48.3, 23.56, 16.73, 11.41 = 100.0

   For probability spread 5
   Results 54.22, 19.31, 14.65, 11.82 = 100.0


Here is a longer example:

    def weighted_random_index_example
      arr = ['apples', 'oranges', 'bananas', 'guava']
      puts "Sample array = [#{arr.join(",")}]"
      p = [0.5,0.25, 0.15, 0.10]
      puts "Probability that each will show up [#{p.join(', ')}]"
      puts "1000 runs..."
      res = Array.new(arr.size).fill(0)
      1000.times do |t|
        res[arr.weighted_random_index(p)] += 1
      end
      res2 = res.collect {|x| (x/1000.0) * 100}
      puts "Results:"
      4.times do |t|
        puts "    #{arr[t]}: #{res2[t]}%"
      end
      
      puts ""
      puts "You can also use more spread out probability arrays"
      p = arr.probability(3)
      puts "Probability that each will show up with a spread of 3 [#{p.join(', ')}]"
      puts "1000 runs..."
      res = Array.new(arr.size).fill(0)
      1000.times do |t|
        res[arr.weighted_random_index(p)] += 1
      end
      res2 = res.collect {|x| (x/1000.0) * 100}
      puts "Results:"
      4.times do |t|
        puts "    #{arr[t]}: #{res2[t]}%"
      end
      
      puts ""
      puts "You can also just get selected indexes"
      puts "arr.get_weighted_random_indexes(3,p) = [#{arr.get_weighted_random_indexes(3,p).join(', ')}]"
      
      puts "The probability spread will depend on the number of items in your array - for an array of 4 it looks like this:"
      8.times do |t|
        puts "  probability(#{t+2}):  [#{arr.probability(t+2).collect {|x| sprintf('%0.5f',x)}.join(', ')}]"
      end
      return nil
    end
    weighted_random_index_example

Outputs...
Sample array = [apples,oranges,bananas,guava]
Probability that each will show up [0.5, 0.25, 0.15, 0.1]
1000 runs...
Results:
    apples: 49.1%
    oranges: 38.3%
    bananas: 11.3%
    guava: 1.3%

You can also use more spread out probability arrays
Probability that each will show up with a spread of 3 [0.333333333333333, 0.111111111111111, 0.037037037037037, 0.0123456790123457]
1000 runs...
Results:
    apples: 43.6%
    oranges: 29.2%
    bananas: 18.0%
    guava: 9.2%

You can also just get selected indexes
arr.get_weighted_random_indexes(3,p) = [0, 1, 3]
The probability spread will depend on the number of items in your array - for an array of 4 it looks like this:
  probability(2):  [0.50000, 0.25000, 0.12500, 0.06250]

Ruby dictionary username generation

Generate a new random name from dictionary words.

DICT_PATH = '/usr/share/dict/words'
DICT_SIZE = 234936

def self.generated_name words = 2, length = 23
  name = 'a'*(length+1)
  while name.length > length
    name = (1..words).map{%x[sed -n '#{rand(DICT_SIZE)} {p;q;}' '#{DICT_PATH}'].chomp.capitalize}.join
  end
end
« Newer Snippets
Older Snippets »
Showing 1-10 of 42 total  RSS