summaryrefslogtreecommitdiff
path: root/lib/std/rand.myr
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-12-14 22:02:55 -0800
committerOri Bernstein <ori@eigenstate.org>2015-12-14 22:02:55 -0800
commit607b2662bc1093833bfea0c49236f098271cf348 (patch)
tree84ac0dab9f80e0071667e9c7327cf749055709eb /lib/std/rand.myr
parent288e48ee60b049206d1dd8ce250bf09c92452e7b (diff)
downloadmc-607b2662bc1093833bfea0c49236f098271cf348.tar.gz
Fix libstd rng for unsigned vlaues, and add tests.
Diffstat (limited to 'lib/std/rand.myr')
-rw-r--r--lib/std/rand.myr9
1 files changed, 5 insertions, 4 deletions
diff --git a/lib/std/rand.myr b/lib/std/rand.myr
index 2e64fe7..566a4cf 100644
--- a/lib/std/rand.myr
+++ b/lib/std/rand.myr
@@ -1,4 +1,5 @@
use "die.use"
+use "extremum.use"
use "assert.use"
use "types.use"
use "alloc.use"
@@ -113,11 +114,11 @@ generic rngrand = {rng, lo, hi -> @a::(integral,numeric)
var maxrand
var val
- assert(hi - lo > 0, "rand.myr: range for random values must be >= 1")
+ span = abs(hi - lo)
+ assert(span > 0, "rand.myr: range for random values must be >= 1, got hi={}, lo={}\n", hi, lo)
- span = hi - lo
- maxrand = (1 << (8*sizeof(@a))) - 1 /* max for signed value */
- if maxrand < 0 /* signed */
+ maxrand = ~0
+ if maxrand < 0 /* if we're signed the maximum value is different */
maxrand = (1 << (8*sizeof(@a)-1)) - 1 /* max for signed value */
;;