]> Repositorios git - scryer-prolog.git/commitdiff
introduce and use weighted_maybe/2
authorMarkus Triska <[email protected]>
Sun, 8 Dec 2024 20:21:05 +0000 (21:21 +0100)
committerMarkus Triska <[email protected]>
Sun, 8 Dec 2024 20:33:03 +0000 (21:33 +0100)
Many thanks to @hurufu for the very nice definition!

This is until library(random) provides this functionality,
and a good name is found for it. See #2670.

src/lib/clpb.pl

index a20694d03b16fb8eb11493c94d6ebd030779ba72..7cf9e803b097be4defb00a7d8f7a364dbd58532b 100644 (file)
@@ -1550,11 +1550,15 @@ random_bindings(VNum, Node) -->
         { node_var_low_high(Node, Var, Low, High),
           bdd_count(Node, VNum, Total),
           bdd_count(Low, VNum, LCount) },
-        (   { maybe(LCount, Total) } ->
+        (   { weighted_maybe(LCount, Total) } ->
             [Var=0], random_bindings(VNum, Low)
         ;   [Var=1], random_bindings(VNum, High)
         ).
 
+weighted_maybe(K, N) :-
+        random_integer(0, N, X),
+        X < K.
+
 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Find solutions with maximum weight.
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */