Skip to content

Commit 721cdf1

Browse files
fixes (rand-int 0) case
1 parent 7dd29ae commit 721cdf1

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

pixie/stdlib.pxi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,9 @@ returns true"
554554
(defn rand-int
555555
{:doc "random integer between 0 (inclusive) and n (exclusive)"}
556556
[n]
557-
(rem (rand) n))
557+
(if (zero? n)
558+
0
559+
(rem (rand) n)))
558560

559561
(defn =
560562
{:doc "Returns true if all the arguments are equivalent. Otherwise, returns false. Uses

tests/pixie/tests/test-stdlib.pxi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,9 @@
330330

331331
(t/deftest test-rand-int
332332
(let [vs (repeatedly 10 #(rand-int 4))]
333-
(t/assert (every? #(and (>= % 0) (< % 4)) vs))))
333+
(t/assert (every? #(and (>= % 0) (< % 4)) vs)))
334+
(let [vs (repeatedly 10 #(rand-int 0))]
335+
(t/assert (every? zero? vs))))
334336

335337
(t/deftest test-some
336338
(t/assert= (some even? [2 4 6 8]) true)

0 commit comments

Comments
 (0)