! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien.data arrays assocs byte-arrays
-byte-vectors combinators combinators.short-circuit fry
-hashtables hashtables.private hash-sets hints io.backend
-io.binary kernel locals math math.bitwise math.constants
-math.functions math.order math.ranges namespaces sequences
-sequences.private sets summary system typed vocabs ;
+combinators combinators.short-circuit hash-sets hashtables
+hashtables.private kernel math math.bitwise math.constants
+math.functions math.order namespaces sequences sequences.private
+sets summary system vocabs ;
QUALIFIED-WITH: alien.c-types c
QUALIFIED-WITH: sets sets
IN: random
SYMBOL: secure-random-generator
SYMBOL: random-generator
-GENERIC# seed-random 1 ( obj seed -- obj )
+GENERIC#: seed-random 1 ( obj seed -- obj )
GENERIC: random-32* ( obj -- n )
GENERIC: random-bytes* ( n obj -- byte-array )
M: object random-bytes*
- [ integer>fixnum-strict [ <byte-array> ] keep ] dip
+ [ integer>fixnum-strict [ (byte-array) ] keep ] dip
[ over 4 >= ] [
[ 4 - ] dip
[ random-32* 2over c:int c:set-alien-value ] keep
M: no-random-number-generator summary
drop "Random number generator is not defined." ;
-M: f random-bytes* ( n obj -- * ) no-random-number-generator ;
+M: f random-bytes* no-random-number-generator ;
-M: f random-32* ( obj -- * ) no-random-number-generator ;
+M: f random-32* no-random-number-generator ;
: random-32 ( -- n )
random-generator get random-32* ;
[ 32 shift obj random-32* + ] [ 32 + ] [ 32 - ] tri*
] while drop [ m * ] [ neg shift ] bi* ; inline
-GENERIC# (random-integer) 1 ( m obj -- n )
-M: fixnum (random-integer) ( m obj -- n ) random-integer-loop ;
-M: bignum (random-integer) ( m obj -- n ) random-integer-loop ;
+GENERIC#: (random-integer) 1 ( m obj -- n )
+M: fixnum (random-integer) random-integer-loop ;
+M: bignum (random-integer) random-integer-loop ;
: random-integer ( m -- n )
random-generator get (random-integer) ;
[ array-nth ] [ [ 1 + ] dip array-nth ] 2bi 2array
] if-zero ;
-M: unordered-set random members random ;
+M: sets:set random members random ;
M: hash-set random
dup cardinality [ drop f ] [
: sample ( seq n -- seq' )
2dup [ length ] dip < [ too-many-samples ] when
- [ [ length iota >array ] dip [ randomize-n-last ] keep tail-slice* ]
+ [ [ length <iota> >array ] dip [ randomize-n-last ] keep tail-slice* ]
[ drop ] 2bi nths-unsafe ;
: delete-random ( seq -- elt )