beetlebug▾
/

tcl::mathfunc srand

Sets the seed of the pseudo-random generator and gives its first value.

tcl::mathfunc::srand SEED
expr {srand(SEED)}

Description

Sets the seed of the generator of rand from SEED and gives the first value of the new sequence. The same seed gives the same sequence. A float seed uses its integer part, so srand(1.5) is srand(1). A seed of any size is valid, and the generator uses its low bits.

Arguments

ArgumentTypeDefaultDescription
SEEDnumberrequiredThe seed: an integer, or a float with its fraction part dropped.

Returns

A float greater than 0 and less than 1: the first value of the new sequence.

Examples

% expr {srand(1)}
0.8597941207808165

% expr {srand(1.5)}
0.8597941207808165

% expr {srand(2)}
0.7195882416193794

Errors

# Tcl 9.1 only
% expr {srand("x")}
cannot use non-numeric string "x" as operand of "srand"
-errorcode ARITH DOMAIN {non-numeric string}

# Tcl 8.6 only
% expr {srand("x")}
can't use non-numeric string as operand of "srand"
-errorcode ARITH DOMAIN {non-numeric string}

% expr {srand()}
not enough arguments for math function "srand"
-errorcode TCL WRONGARGS

Facts

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

See also