SIMPSON coding examples

Random number with Gaussian distribution

by Thomas Vosegaard

This Tcl procedure generates a random number with Gaussian distribution.

1
2
3
4
5
6
7
8
9
10
11
12
13
proc grand {} {
  # Generate gaussian noise
  set s 2
  while {$s >= 1} {
    set v1 [expr 2.0*rand()-1.0]
    set v2 [expr 2.0*rand()-1.0]
    set s [expr $v1*$v1 + $v2*$v2]
  }
  if {$s != 0} {
    set s [expr $v1*sqrt(-2.0*log($s)/$s)]
  }
  return $s
}
Add comment

Snippets version 1.0.1