From 413395866f1caa5046164b4e43ceed64ca60103a Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Wed, 2 Aug 2023 16:58:29 +0800 Subject: [PATCH] fixes #22360; compare with the half of randMax (#22361) * fixes #22360; compare with the half of randMax * add a test (cherry picked from commit f3a7622514f24740c6b33f0c37ebe6339ad5b70d) --- lib/pure/random.nim | 5 +---- tests/stdlib/trandom.nim | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/pure/random.nim b/lib/pure/random.nim index 422f42a8b8..88616594b1 100644 --- a/lib/pure/random.nim +++ b/lib/pure/random.nim @@ -381,10 +381,7 @@ proc rand*[T: Ordinal](r: var Rand; t: typedesc[T]): T {.since: (1, 7, 1).} = when T is range or T is enum: result = rand(r, low(T)..high(T)) elif T is bool: - whenJsNoBigInt64: - result = (r.next or 0) < 0 - do: - result = cast[int64](r.next) < 0 + result = r.next < randMax div 2 else: whenJsNoBigInt64: result = cast[T](r.next shr (sizeof(uint)*8 - sizeof(T)*8)) diff --git a/tests/stdlib/trandom.nim b/tests/stdlib/trandom.nim index 8784b33ee4..920d429d4f 100644 --- a/tests/stdlib/trandom.nim +++ b/tests/stdlib/trandom.nim @@ -282,3 +282,21 @@ block: # bug #17898 for j in 0..