beetlebug▾
/

tcl::mathfunc rand

Gives a pseudo-random float from 0 to 1.

tcl::mathfunc::rand
expr {rand()}

Description

Gives the next value of the pseudo-random generator of the interpreter: a float greater than 0 and less than 1. srand sets the seed and gives the first value.

A new interpreter has a fixed seed, so the values are the same in each run of a script. The first rand() of a new interpreter is the same as the result of srand(1).

Returns

A float greater than 0 and less than 1.

Examples

# The first value of a new interpreter.
% expr {rand()}
0.8597941207808165

% expr {srand(42)}
0.825248325378704

# The next value after srand(42).
% expr {rand()}
0.8253764462153473

Errors

% expr {rand(1)}
too many arguments for math function "rand"
-errorcode TCL WRONGARGS

Differences from Tcl

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also