random: Add random-integers and random-units words.
parent
55b127f96b
commit
46c5d64251
|
@ -1,4 +1,4 @@
|
||||||
USING: help.markup help.syntax math kernel sequences ;
|
USING: help.markup help.syntax math kernel sequences arrays ;
|
||||||
IN: random
|
IN: random
|
||||||
|
|
||||||
HELP: seed-random
|
HELP: seed-random
|
||||||
|
@ -46,6 +46,34 @@ HELP: random-bytes
|
||||||
}
|
}
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
|
HELP: random-integers
|
||||||
|
{ $values { "length" integer } { "n" integer } { "sequence" array } }
|
||||||
|
{ $description "Outputs an array with " { $snippet "length" } " random integers from [0,n)." }
|
||||||
|
{ $examples
|
||||||
|
{ $unchecked-example "USING: prettyprint random ;"
|
||||||
|
"10 100 random-integers ."
|
||||||
|
"{ 32 62 71 89 54 12 57 57 10 19 }"
|
||||||
|
}
|
||||||
|
} ;
|
||||||
|
|
||||||
|
HELP: random-units
|
||||||
|
{ $values { "length" integer } { "sequence" array } }
|
||||||
|
{ $description "Outputs an array with " { $snippet "length" } " random uniform floats from [0,1]." }
|
||||||
|
{ $examples
|
||||||
|
{ $unchecked-example "USING: prettyprint random ;"
|
||||||
|
"7 random-units ."
|
||||||
|
"{
|
||||||
|
0.1881956429982787
|
||||||
|
0.9063571897519639
|
||||||
|
0.9550470241550406
|
||||||
|
0.6289397941552234
|
||||||
|
0.9441213853903183
|
||||||
|
0.7673290082934152
|
||||||
|
0.573743749061385
|
||||||
|
}"
|
||||||
|
}
|
||||||
|
} ;
|
||||||
|
|
||||||
HELP: random-bits
|
HELP: random-bits
|
||||||
{ $values { "numbits" integer } { "r" "a random integer" } }
|
{ $values { "numbits" integer } { "r" "a random integer" } }
|
||||||
{ $description "Outputs an random integer n bits in length." } ;
|
{ $description "Outputs an random integer n bits in length." } ;
|
||||||
|
@ -130,6 +158,8 @@ $nl
|
||||||
{ $subsections sample }
|
{ $subsections sample }
|
||||||
"Deleting a random element from a sequence:"
|
"Deleting a random element from a sequence:"
|
||||||
{ $subsections delete-random }
|
{ $subsections delete-random }
|
||||||
|
"Sequences of random numbers:"
|
||||||
|
{ $subsections random-bytes random-integers random-units }
|
||||||
"Random numbers with " { $snippet "n" } " bits:"
|
"Random numbers with " { $snippet "n" } " bits:"
|
||||||
{ $subsections
|
{ $subsections
|
||||||
random-bits
|
random-bits
|
||||||
|
|
|
@ -106,6 +106,12 @@ ERROR: too-many-samples seq n ;
|
||||||
: random-unit ( -- n )
|
: random-unit ( -- n )
|
||||||
0.0 1.0 uniform-random-float ; inline
|
0.0 1.0 uniform-random-float ; inline
|
||||||
|
|
||||||
|
: random-units ( length -- sequence )
|
||||||
|
[ random-unit ] replicate ;
|
||||||
|
|
||||||
|
: random-integers ( length n -- sequence )
|
||||||
|
'[ _ random ] replicate ;
|
||||||
|
|
||||||
: (cos-random-float) ( -- n )
|
: (cos-random-float) ( -- n )
|
||||||
0. 2pi uniform-random-float cos ;
|
0. 2pi uniform-random-float cos ;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue