sample ( seq n -- seq' ) - Factor Documentation

sample ( seq n -- seq' )
Generating random integers



Vocabulary
random

Inputs
seq a sequence
n an integer


Outputs
seq' a sequence


Word description
Takes n samples at random without replacement from a sequence. Throws an error if n is longer than the sequence.

Examples
USING: random prettyprint ; { 1 2 3 } 2 sample .
{ 3 2 }


Definition
USING: arrays kernel math sequences sequences.private ;

IN: random

: sample ( seq n -- seq' )
2dup [ length ] dip < [ too-many-samples ] when [
[ length <iota> >array ] dip [ randomize-n-last ] keep
tail-slice*
] keepd nths-unsafe ;

AltStyle によって変換されたページ (->オリジナル) /