summaryrefslogtreecommitdiff
path: root/lib/std/rand.myr
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-09-18 02:05:13 -0700
committerOri Bernstein <ori@eigenstate.org>2015-09-18 02:06:35 -0700
commitda45fcd5ccafb05f813c989a379dd6920e517c53 (patch)
tree8119203ca06efebb524c0487fdfa2c2c3f8e207d /lib/std/rand.myr
parent96f7ecd57ed79995544a125e752c9711aeedbcb8 (diff)
downloadmc-da45fcd5ccafb05f813c989a379dd6920e517c53.tar.gz
Remove 'long' type.
It wasn't ever actually used.
Diffstat (limited to 'lib/std/rand.myr')
-rw-r--r--lib/std/rand.myr36
1 files changed, 25 insertions, 11 deletions
diff --git a/lib/std/rand.myr b/lib/std/rand.myr
index 6ca23e6..69d66bc 100644
--- a/lib/std/rand.myr
+++ b/lib/std/rand.myr
@@ -1,6 +1,8 @@
use "die.use"
use "types.use"
use "alloc.use"
+use "now.use"
+
/*
Translated from C by Ori Bernstein
*/
@@ -48,11 +50,13 @@ pkg std =
type rng
const mksrng : (seed : uint32 -> rng#)
- const delrng : (rng : rng# -> void)
- generic rand : (rng : rng#, lo : @a::(numeric,integral), hi : @a::(numeric,integral) -> @a::(numeric,integral))
- generic randN : (rng : rng# -> @a::(numeric,integral))
- const randbytes : (rng : rng#, buf : byte[:] -> size)
- const rand32 : (rng : rng# -> uint32)
+ const freerng : (rng : rng# -> void)
+
+ generic rand : (lo : @a::(numeric,integral), hi : @a::(numeric,integral) -> @a::(numeric,integral))
+
+ generic rngrand : (rng : rng#, lo : @a::(numeric,integral), hi : @a::(numeric,integral) -> @a::(numeric,integral))
+ generic rngrandnum : (rng : rng# -> @a::(numeric,integral))
+ const rngrandbytes : (rng : rng#, buf : byte[:] -> size)
;;
type rng = struct
@@ -60,6 +64,12 @@ type rng = struct
i : uint32
;;
+var _rng : rng#
+
+const __init__ = {
+ _rng = mksrng(now() castto(uint32))
+}
+
/* allocates and initializes a random number generator */
const mksrng = {seed
var rng
@@ -69,10 +79,14 @@ const mksrng = {seed
-> rng
}
-const delrng = {rng
+const freerng = {rng
free(rng)
}
+generic rand = {lo, hi
+ -> rngrand(_rng, lo, hi)
+}
+
/* initializes a random number generator from the seed `seed`. */
const init = {rng, seed
for var i = 0; i < 624; i++
@@ -88,7 +102,7 @@ const init = {rng, seed
nonempty, and the difference between hi and lo must be
less then 2^(type_bits - 1)
*/
-generic rand = {rng, lo, hi -> @a::(integral,numeric)
+generic rngrand = {rng, lo, hi -> @a::(integral,numeric)
var span, lim
var maxrand
var val
@@ -102,9 +116,9 @@ generic rand = {rng, lo, hi -> @a::(integral,numeric)
;;
lim = (maxrand/span)*span
- val = (randN(rng) & maxrand)
+ val = (rngrandnum(rng) & maxrand)
while val > lim
- val = (randN(rng) & maxrand)
+ val = (rngrandnum(rng) & maxrand)
;;
-> val % span + lo
}
@@ -114,7 +128,7 @@ generic rand = {rng, lo, hi -> @a::(integral,numeric)
random number generator `rng`. The returned value
may be negative, if the type is signed.
*/
-generic randN = {rng -> @a::(integral,numeric)
+generic rngrandN = {rng -> @a::(integral,numeric)
var val
val = 0
@@ -144,7 +158,7 @@ const rand32 = {rng
-> x ^ (x >> 18)
}
-const randbytes = {rng, buf
+const rngrandbytes = {rng, buf
var n, r
n = 0