We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 4f84bf4 + df0cc46 commit d79df5aCopy full SHA for d79df5a
motoko/random_maze/src/random_maze/Main.mo
@@ -41,16 +41,14 @@ actor {
41
func chooseMax(f : Random.Finite, max : Nat) : ? Nat {
42
assert max > 0;
43
do ? {
44
- if (max == 1) return ? 0;
45
- var k = bit(f.coin()!);
46
- var n = max / 2;
47
- while (n > 1) {
48
- k := k * 2 + bit(f.coin()!);
49
- n := n / 2;
+ var n = max - 1 : Nat;
+ var k = 0;
+ while (n != 0) {
+ k *= 2;
+ k += bit(f.coin()!);
+ n /= 2;
50
};
51
- if (k < max)
52
- return ? k
53
- else chooseMax(f, max) !; // retry
+ if (k < max) k else chooseMax(f, max)!;
54
55
56
0 commit comments