Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Random numbers in Tcl/Tk

20 views
Skip to first unread message

Torsten Mohr

unread,
May 2, 1998, 3:00:00 AM5/2/98
to

Hi,

i want to use random numbers in a vocabulary trainer,
but i can't find anything like that in Tcl/Tk.

I'm using Tcl/Tk under Linux, so using the environment
variable $RANDOM of the Korn shell could be a solution,
but i don't know how to access it.
This would also make the script non-portable.

Is there a way to do this?

Thanks for any hints,
tm...@stuttgart.netsurf.de

J.M. Ivler

unread,
May 2, 1998, 3:00:00 AM5/2/98
to

Torsten Mohr <tm...@schleim.ludwigsburg.netsurf.de> wrote:
> i want to use random numbers in a vocabulary trainer,
> but i can't find anything like that in Tcl/Tk.

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 compare [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
}
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)))]
}
##
set rannum [random 3]

this will set your random number between 0-2 inclusive.


Cameron Laird

unread,
May 3, 1998, 3:00:00 AM5/3/98
to

In article <6ifouf$m2$1...@schleim.ludwigsburg.netsurf.de>,
Torsten Mohr <tm...@stuttgart.netsurf.de> wrote:
>Hi,

>
>i want to use random numbers in a vocabulary trainer,
>but i can't find anything like that in Tcl/Tk.
>
>I'm using Tcl/Tk under Linux, so using the environment
>variable $RANDOM of the Korn shell could be a solution,
>but i don't know how to access it.
>This would also make the script non-portable.
>
>Is there a way to do this?
.
.
.
Yes.

<URL:http://www.altavista.digital.com/cgi-bin/query?pg=q&text=yes
&what=news&kl=XX&q=%2Brandom++%2B%22comp.lang.tcl%22&act=search>
--

Cameron Laird http://starbase.neosoft.com/~claird/home.html
cla...@NeoSoft.com +1 713 996 8546 FAX

Andreas Kupries

unread,
May 3, 1998, 3:00:00 AM5/3/98
to

tm...@schleim.ludwigsburg.netsurf.de (Torsten Mohr) writes:

> i want to use random numbers in a vocabulary trainer,
> but i can't find anything like that in Tcl/Tk.

Since tcl 8.x the 'expr' command provides a 'rand' function.

For a version below that there should be several generators on the net
and written in pure tcl. The only URL I have in my notes is

http://www.psg.com/~joem/tcl/faq.html#RandomNumbers

--
Sincerely,
Andreas Kupries <a.ku...@westend.com>
<http://www.westend.com/~kupries/>
-------------------------------------------------------------------------------

0 new messages