Change the interface for random numbers.

This commit is contained in:
Elliot Glaysher 2017-09-29 21:26:50 -07:00
parent 08962cfd63
commit 7d46bccdfc
2 changed files with 49 additions and 34 deletions

View File

@ -92,7 +92,9 @@
|= {min/@ max/@}
|= c/@uvJ
^- @
(add min (~(rad og c) (sub max min)))
=+ gen=(random:new-hoon c)
=^ num gen (range:gen min max)
num
::
++ generate-map
:> generator which will produce a map with {count} random pairs.
@ -106,8 +108,8 @@
^- (map @ud @ud)
?: =(i count)
m
=^ first gen (rads:gen 100)
=^ second gen (rads:gen 100)
=^ first gen (range:gen 0 100)
=^ second gen (range:gen 0 100)
$(m (insert:mp:new-hoon m first second), i +(i))
::
:: || %test
@ -611,8 +613,8 @@
|-
?: =(i 40)
%.y
=^ key gen (rads:gen 100)
=^ value gen (rads:gen 100)
=^ key gen (range:gen 0 100)
=^ value gen (range:gen 0 100)
=. a %^ alter-with-key a key
|= {key/@ud current/(maybe @ud)}
^- (maybe @ud)

View File

@ -1432,38 +1432,51 @@
==
--
++ random
:> produces a core which produces random numbers.
:: todo: think hard about whether this interface really makes any sense;
:: this is marginally better than ++og for rads usage, but still isn't good.
:> produces a core which produces random numbers.
:>
:> random numbers are generated through repeated sha-256 operations.
:>
:> this design forces implementation details to be hidden, forces users to
:> go through =^. this should be less error prone for pulling out multiple
:> random numbers, at the cost of making getting a single random number
:> slightly more cumbersome.
:>
:> =+ gen=(random eny)
:> =^ first gen (range:gen 0 10)
:> =^ second gen (range:gen 0 10)
|= a/@
:: note: interior was copied verbatim from ++og.
|%
++ rad :: random in range
|= b/@ ^- @
=+ c=(raw (met 0 b))
?:((lth c b) c $(a +(a)))
=> |%
++ raw :: random bits
|= b/@ ^- @
%+ can
0
=+ c=(shas %og-a (mix b a))
|- ^- (list {@ @})
?: =(0 b)
~
=+ d=(shas %og-b (mix b (mix a c)))
?: (lth b 256)
[[b (end 0 b d)] ~]
[[256 d] $(c d, b (sub b 256))]
::
++ rad :: random in range
|= b/@ ^- @
=+ c=(raw (met 0 b))
?:((lth c b) c $(a +(a)))
--
^? |%
++ range
:> returns a random number in the range [start, end], and generator.
|= {start/@ end/@}
?: (gte start end)
~_(leaf+"invalid range" !!)
=+ offset=(sub end start)
=+ r=(rad offset)
[(add start r) +>.$(a (shas %og-s (mix a r)))]
::
++ rads :: random continuation
++ bits
:> returns {b} bits in the range, and generator.
|= b/@
=+ r=(rad b)
[r +>.$(a (shas %og-s (mix a r)))]
::
++ raw :: random bits
:: ~/ %raw
|= b/@ ^- @
%+ can
0
=+ c=(shas %og-a (mix b a))
|- ^- (list {@ @})
?: =(0 b)
~
=+ d=(shas %og-b (mix b (mix a c)))
?: (lth b 256)
[[b (end 0 b d)] ~]
[[256 d] $(c d, b (sub b 256))]
::
++ raws :: random bits
|= b/@ :: continuation
=+ r=(raw b)
[r +>.$(a (shas %og-s (mix a r)))]
--