Hi,
Just a comment...
Getting good random numbers is a tricky buisness. If
you're writing a game that
just needs to seem unpredictable, this is great If you're
doing calculations, like Monte-Carlo,
and _really_ need random numbers, you should at least use
the C random function. (not rand)
Welch givesan example of wrapping the C random function with
tcl on page 343 of the first edition.
For some applications not even random is good enough!
Bob.
>----------
>From: Phanidhar[SMTP:[EMAIL PROTECTED]]
>Sent: Friday, February 14, 1997 3:46 AM
>To: [EMAIL PROTECTED]
>Cc: Vtcl-SA
>Subject: Re: Random Function for TCL/TK
>
>[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
>