Skip to content

Commit 9dafa32

Browse files
committed
random: support "random" on floats (uses uniform-random-float).
1 parent f7d58ff commit 9dafa32

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

basis/random/random.factor

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
! Copyright (C) 2008 Doug Coleman.
22
! See http://factorcode.org/license.txt for BSD license.
3-
USING: accessors alien.c-types alien.data arrays assocs
4-
byte-arrays byte-vectors combinators combinators.short-circuit
5-
fry hashtables hashtables.private hints io.backend io.binary
3+
USING: accessors alien.data arrays assocs byte-arrays
4+
byte-vectors combinators combinators.short-circuit fry
5+
hashtables hashtables.private hints io.backend io.binary
66
kernel locals math math.bitwise math.constants math.functions
77
math.order math.ranges namespaces sequences sequences.private
88
sets summary system typed vocabs ;
9+
QUALIFIED-WITH: alien.c-types c
910
IN: random
1011

1112
SYMBOL: system-random-generator
@@ -18,10 +19,10 @@ GENERIC: random-bytes* ( n tuple -- byte-array )
1819

1920
M: object random-bytes* ( n tuple -- byte-array )
2021
[ [ <byte-vector> ] keep 4 /mod ] dip
21-
[ pick '[ _ random-32* int <ref> _ push-all ] times ]
22+
[ pick '[ _ random-32* c:int <ref> _ push-all ] times ]
2223
[
2324
over zero?
24-
[ 2drop ] [ random-32* int <ref> swap head append! ] if
25+
[ 2drop ] [ random-32* c:int <ref> swap head append! ] if
2526
] bi-curry bi* B{ } like ;
2627

2728
HINTS: M\ object random-bytes* { fixnum object } ;
@@ -112,7 +113,7 @@ ERROR: too-many-samples seq n ;
112113
<PRIVATE
113114

114115
: (uniform-random-float) ( min max obj -- n )
115-
[ 4 4 ] dip [ random-bytes* uint deref >float ] curry bi@
116+
[ 4 4 ] dip [ random-bytes* c:uint deref >float ] curry bi@
116117
2.0 32 ^ * +
117118
[ over - 2.0 -64 ^ * ] dip
118119
* + ; inline
@@ -122,6 +123,8 @@ PRIVATE>
122123
: uniform-random-float ( min max -- n )
123124
random-generator get (uniform-random-float) ; inline
124125

126+
M: float random [ f ] [ 0.0 swap uniform-random-float ] if-zero ;
127+
125128
: random-unit ( -- n )
126129
0.0 1.0 uniform-random-float ; inline
127130

0 commit comments

Comments
 (0)