Handbook
Glossary
sample ( seq n -- seq' )
Generating random integers
Prev:
randomize ( seq -- randomized )
Next:
delete-random ( seq -- elt )
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*
]
[
drop
]
2bi
nths-unsafe
;