Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Ruby Programming Language

Some probabilities fun


Hello all.

Only wanted to show two short probability tricks, which can make use in
games or other "random" environments.

-----------------
#Trick 1: use ~ sign to say "approximately"

class Numeric
   LEVEL = 0.3
   def ~
     self + self * rand(LEVEL) - self* LEVEL/2.0
   end
end

#testing it:

10.times{puts "=" * ~15.0}

#now you shall see 10 bars with random lengthes in range 15 +-15%
#can be used, for example, in game charactes move: go(~10.0), turn(~15.0)

#!warning: we still leave Fixnum#~ as byte negation
#you can rewrite it too, but on your own risk!

----------------
#Trick 2: indeterminate event

def probable(pct)
  yield if rand(0.99999999) < pct
end

res = 0

100.times{
  probable(0.4){res += 1}

}

#100 times increase res with 40% probability

p res
#you should see something between 30 and 50...

Isn't cool? :)

V.

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc