From 7d46bccdfc7cbcaaaf1dd5a8abb90ab76d16712a Mon Sep 17 00:00:00 2001 From: Elliot Glaysher Date: Fri, 29 Sep 2017 21:26:50 -0700 Subject: [PATCH] Change the interface for random numbers. --- gen/test.hoon | 12 ++++---- lib/new-hoon.hoon | 71 ++++++++++++++++++++++++++++------------------- 2 files changed, 49 insertions(+), 34 deletions(-) diff --git a/gen/test.hoon b/gen/test.hoon index ea7a49f773..476393f2d8 100644 --- a/gen/test.hoon +++ b/gen/test.hoon @@ -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) diff --git a/lib/new-hoon.hoon b/lib/new-hoon.hoon index 3d086eeada..e12957e43d 100644 --- a/lib/new-hoon.hoon +++ b/lib/new-hoon.hoon @@ -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)))] --