factor/apps/random-tester/random.factor

88 lines
2.6 KiB
Factor
Raw Permalink Normal View History

USING: kernel math sequences namespaces errors hashtables words
arrays parser compiler syntax io tools prettyprint optimizer
inference ;
2006-01-21 01:23:41 -05:00
IN: random-tester
! Tweak me
2006-07-24 12:23:00 -04:00
: max-length 15 ; inline
2006-01-21 01:23:41 -05:00
: max-value 1000000000 ; inline
: 10% ( -- bool ) 10 random-int 8 > ;
: 20% ( -- bool ) 10 random-int 7 > ;
: 30% ( -- bool ) 10 random-int 6 > ;
: 40% ( -- bool ) 10 random-int 5 > ;
: 50% ( -- bool ) 10 random-int 4 > ;
: 60% ( -- bool ) 10 random-int 3 > ;
: 70% ( -- bool ) 10 random-int 2 > ;
: 80% ( -- bool ) 10 random-int 1 > ;
: 90% ( -- bool ) 10 random-int 0 > ;
2006-01-21 01:23:41 -05:00
! varying bit-length random number
: random-bits ( n -- int )
random-int 2 swap ^ random-int ;
: random-seq ( -- seq )
{ [ ] { } V{ } "" } pick-one
2006-01-21 01:23:41 -05:00
[ max-length random-int [ max-value random-int , ] times ] swap make ;
2006-01-23 18:27:52 -05:00
: random-string
[ max-length random-int [ max-value random-int , ] times ] "" make ;
2006-01-21 01:23:41 -05:00
SYMBOL: special-integers
[ { -1 0 1 } % most-negative-fixnum , most-positive-fixnum , first-bignum , ]
2006-07-24 12:23:00 -04:00
{ } make \ special-integers set-global
2006-01-21 01:23:41 -05:00
: special-integers ( -- seq ) \ special-integers get ;
SYMBOL: special-floats
2006-01-28 16:02:54 -05:00
[ { 0.0 -0.0 } % e , pi , 1./0. , -1./0. , 0./0. , epsilon , epsilon neg , ]
2006-07-24 12:23:00 -04:00
{ } make \ special-floats set-global
2006-01-21 01:23:41 -05:00
: special-floats ( -- seq ) \ special-floats get ;
SYMBOL: special-complexes
[
{ -1 0 1 i -i } %
2006-01-23 18:59:33 -05:00
e , e neg , pi , pi neg ,
0 pi rect> , 0 pi neg rect> , pi neg 0 rect> , pi pi rect> ,
2006-01-21 01:23:41 -05:00
pi pi neg rect> , pi neg pi rect> , pi neg pi neg rect> ,
e neg e neg rect> , e e rect> ,
2006-07-24 12:23:00 -04:00
] { } make \ special-complexes set-global
2006-01-21 01:23:41 -05:00
: special-complexes ( -- seq ) \ special-complexes get ;
: random-fixnum ( -- fixnum )
most-positive-fixnum random-int 1+ coin-flip [ neg 1- ] when >fixnum ;
: random-bignum ( -- bignum )
400 random-bits first-bignum + coin-flip [ neg ] when ;
2006-07-24 12:23:00 -04:00
: random-integer ( -- n )
2006-01-21 01:23:41 -05:00
coin-flip [
2006-07-24 12:23:00 -04:00
random-fixnum
] [
coin-flip [ random-bignum ] [ special-integers pick-one ] if
2006-07-24 12:23:00 -04:00
] if ;
2006-01-21 01:23:41 -05:00
: random-positive-integer ( -- int )
random-integer dup 0 < [
neg
] [
dup 0 = [ 1 + ] when
] if ;
: random-ratio ( -- ratio )
2006-01-28 13:50:45 -05:00
1000000000 dup [ random-int ] 2apply 1+ / coin-flip [ neg ] when dup [ drop random-ratio ] unless 10% [ drop 0 ] when ;
2006-01-21 01:23:41 -05:00
: random-float ( -- float )
coin-flip [ random-ratio ] [ special-floats pick-one ] if
2006-01-21 01:23:41 -05:00
coin-flip
[ .0000000000000000001 /f ] [ coin-flip [ .00000000000000001 * ] when ] if
>float ;
: random-number ( -- number )
{
[ random-integer ]
[ random-ratio ]
[ random-float ]
} do-one ;
: random-complex ( -- C )
2006-01-21 01:23:41 -05:00
random-number random-number rect> ;