File: random.tcl

package info (click to toggle)
libapache2-mod-rivet 2.3.3-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 5,156 kB
  • ctags: 1,093
  • sloc: xml: 7,696; tcl: 6,939; ansic: 5,682; sh: 4,862; makefile: 199; sql: 91; lisp: 78
file content (34 lines) | stat: -rw-r--r-- 992 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
###
## random [seed | value ]
##
##    Generate a random number using only Tcl code.  This proc tries to
##    emulate what the TclX random function does.  If we don't have TclX
##    though, this is a decent substitute.
##
## Note: random predates the existence of Tcl's built-in rand() function,
## that is a part of the expr command -- programmers should consider using
## Tcl's built-in rand() function as an alternative to this command.
##
## $Id: random.tcl 1212149 2011-12-08 21:57:35Z mxmanghi $
##
###

namespace eval ::rivet {

    proc random {args} {
        global _ran

        if {[llength $args] > 1} {
            set _ran [lindex $args 1]
        } else {
            set period 233280
            if {[info exists _ran]} {
                set _ran [expr { ($_ran*9301 + 49297) % $period }]
            } else {
                set _ran [expr { [clock seconds] % $period } ]
            }
            return [expr { int($args*($_ran/double($period))) } ]
        }
    }

}