#!/usr/local/bin/tclsh
# $Id: random.tcl,v 1.7 2001/05/08 01:36:50 doug Exp $
# http://www.bagley.org/~doug/shootout/
# from Miguel Sofer

trace variable params w make_main
proc make_main {args} {
    global params last
    set randBody [string map $params {
        expr {(100.0 * [set last [expr {($last * IA + IC) % IM}]]) / IM}
    }]
    set mainBody [string map [list randBody $randBody] {
     global argv last
     set N [lindex $argv 0]
     set result 0.0
     while {$N} {
         set result [randBody]
         incr N -1
     }
     puts [format "%.9f" $result]
    }]
    proc main {} $mainBody     
}

set params {IM 139968 IA 3877 IC 29573}
set last 42

main