# Some probabilities fun

**URL:** <https://www.ruby-forum.com/t/some-probabilities-fun/105265>\
**Category:** Ruby\
**Created:** [May 28, 2007, 9:00am UTC](https://www.ruby-forum.com/t/some-probabilities-fun/105265 "2007-05-28T09:00:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Victor\_SZverokSS\_Sh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@Victor\_SZverokSS\_Sh](https://www.ruby-forum.com/u/Victor_SZverokSS_Sh)\
**Post date:** [May 28, 2007, 9:00am UTC](https://www.ruby-forum.com/t/some-probabilities-fun/105265/1 "2007-05-28T09:00:37Z")

</div>

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.
