~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Open Mash Cross Reference
mash/tcl/common/mash-rng.tcl

Component: ~ [ mash ] ~ [ apps ] ~ [ gsm ] ~ [ lib ] ~ [ otcl ] ~ [ srm ] ~ [ tcl8.3 ] ~ [ tclcl ] ~ [ tk8.3 ] ~ [ tutorials ] ~

  1 Class MashRNG
  2 proc exponential {args} {
  3         eval [list MashRNG exponential] $args
  4 }
  5 
  6 proc uniform {args} {
  7         eval [list MashRNG uniform] $args
  8 }
  9 
 10 proc integer {args} {
 11         eval [list MashRNG integer] $args
 12 }
 13 
 14 MashRNG proc init {} {
 15         $self next
 16         #z2 used to create a normal distribution  from a uniform distribution
 17         #using the polar method
 18         $self instvar z2
 19         set z2 0
 20 }
 21 
 22 MashRNG proc seed {seed} {
 23         return [random $seed]
 24 }
 25 
 26 MashRNG proc next-random {} {
 27         return [random]
 28 }
 29 
 30 MashRNG proc uniform {a b} {
 31         expr $a + (($b - $a) * ([$self next-random] * 1.0 / 0x7fffffff))
 32 }
 33 
 34 MashRNG proc integer k {
 35         expr [$self next-random] % abs($k)
 36 }
 37 
 38 MashRNG proc exponential {{mu 1.0}} {
 39         expr - $mu * log(([$self next-random] + 1.0) / (0x7fffffff + 1.0))
 40 }
 41 
 42 MashRNG proc normal {a1 a2 } {
 43         $self instvar z2
 44         if {$z2 !=0 } {
 45                 set z1 $z2
 46                 set z2 0
 47         } else {
 48                 set w 1
 49                 while { $w >= 1.0 } {
 50                         set v1 [expr 2.0 * ([$self next-random] *1.0/0x7fffffff) - 1.0]
 51                         set v2 [expr 2.0 * ([$self next-random] *1.0/0x7fffffff) - 1.0]
 52                         set w  [expr ($v1*$v1+$v2*$v2)]
 53                 }
 54                 set w [expr  sqrt((-2.0*log($w))/$w)]
 55                 set z1 [expr $v1*$w]
 56                 set z2 [expr $v2*$w]
 57         }
 58         expr $a1 + $z1 *$a2
 59 }
 60 
 61 MashRNG proc lognormal {med stddev } {
 62         expr $med *exp($stddev*[$self normal 0.0 1.0])
 63 }
 64 
 65 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.