[EMAIL PROTECTED] wrote:
>
> Quick question is there a Random generator function like rand() in C for TCL.
>
> Thanks for the help.
>
I hope the following code which I got might help you
----------------------------------------------------------
Random number generators
This is available in TclX but heres some tcl implementations
>From Libes "Exploring Expect" p525. See also Welch p52.
# if random is not avalable from libtclx.so
if {[info commands random] == ""} {
# initialize seed.
set _rand [pid]
# random returns a value in the range 0..range-1
proc random {range} {
global _rand
set period 233280
set _rand [expr ($_rand * 9301 + 49297) % $period]
expr int(($_rand/double($period)) * $range)
}
}
>From [EMAIL PROTECTED]
### QUICK AND DIRTY - works on all platforms
set _ran [pid]
proc random {range} {
global _ran
set _ran [expr ($_ran * 9301 + 49297) % 233280]
return [expr int($range * ($_ran / double(233280)))]
}
### SAME SYNTAX AS TCLX random - UNIX dependent
proc random {args} {
global RNG_seed
set max 259200
set argcnt [llength $args]
if { $argcnt < 1 || $argcnt > 2 } {
error "wrong # args: random limit | seed ?seedval?"
}
if [string match [lindex $args 0] seed] {
if { $argcnt == 2 } {
set RNG_seed [lindex $args 1]
} else {
set RNG_seed [expr ([pid]+[file atime /dev/kmem])%$max]
}
return
}
# You could replace '[file atime /dev/kmem]' with '[clock clicks]' in
tcl7.5
# to make it platform independent, but you have to watch for int
overflow.
if ![info exists RNG_seed] {
set RNG_seed [expr ([pid]+[file atime /dev/kmem])%$max]
}
set RNG_seed [expr ($RNG_seed*7141+54773)%$max]
return [expr int([lindex $args 0]*($RNG_seed/double($max)))]
}
----------------------------------------------------------------
phanidhar