Handbook Glossary
factorcode.org
random ( obj -- elt )
Generating random integers

Next:random-32 ( -- n )


Vocabulary
random

Inputs
objan object


Outputs
elta random element


Word description
Outputs a random element of the input object, or outputs f if the object contains no elements.

Examples
USING: random prettyprint ; 10 random .
3

USING: random prettyprint ; SYMBOL: heads SYMBOL: tails { heads tails } random .
heads


Definition
IN: random

GENERIC: random ( obj -- elt )


Methods
USING: assocs random ;

M: assoc random >alist random ;


USING: kernel math random ;

M: float random
[ f ] [ 0.0 swap uniform-random-float ] if-zero ;


USING: accessors hash-sets hashtables.private kernel math random
sequences.private sets ;

M: hash-set random
dup cardinality
[ drop f ] [
[ 0 ] [ array>> ] [ random ] tri* 1 +
[ [ 2dup array-nth tombstone? [ 1 + ] 2dip ] loop ]
times [ 1 - ] dip array-nth
] if-zero ;


USING: accessors arrays assocs hashtables hashtables.private
kernel math random sequences.private ;

M: hashtable random
dup assoc-size
[ drop f ] [
[ 0 ] [ array>> ] [ random ] tri* 1 +
[ [ 2dup array-nth tombstone? [ 2 + ] 2dip ] loop ]
times [ 2 - ] dip
[ array-nth ] [ [ 1 + ] dip array-nth ] 2bi 2array
] if-zero ;


USING: math random random.private ;

M: integer random [ f ] [ random-integer ] if-zero ;


USING: kernel random random.private sequences ;

M: sequence random
[ f ] [ [ length random-integer ] keep nth ] if-empty ;


USING: random sets ;

M: set random members random ;