core: Random.seed with a good default, rename old version to Random.seed-from

This commit is contained in:
hellerve 2018-10-19 10:31:00 +02:00
parent 40af5c3a5a
commit 59f636f051
9 changed files with 210 additions and 7 deletions

View File

@ -13,8 +13,12 @@
(hidden s)
(private s)
(doc seed "seed resets the seed of the random number generator to `new-seed`.")
(defn seed [new-seed]
(doc seed "seed resets the seed of the random number generator.")
(defn seed []
(set! s (Double.from-long (System.nanotime))))
(doc seed-from "seed-from resets the seed of the random number generator to `new-seed`.")
(defn seed-from [new-seed]
(set! s new-seed))
(doc random "random returns a float from 0 to 1.")

View File

@ -5,6 +5,8 @@
(register free (Fn [t] ()))
(doc time "Gets the current system time as an integer.")
(register time (Fn [] Int))
(doc nanotime "Gets the current system time in nanoseconds as a long.")
(register nanotime (Fn [] Long))
(doc sleep-seconds "Sleeps for a specified number of seconds.")
(register sleep-seconds (Fn [Int] ()))
(doc sleep-seconds "Sleeps for a specified number of microseconds.")

View File

@ -24,6 +24,13 @@ void System_sleep_MINUS_micros(int t) {
}
#endif
double System_nanotime() {
struct timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
return 1000000000 * tv.tv_sec + tv.tv_nsec;
}
void System_system(String *command) {
system(*command);
}

View File

@ -162,6 +162,63 @@
the End-Of-File character as a literal.
</p>
</div>
<div class="binder">
<a class="anchor" href="#SEEK-CUR">
<h3 id="SEEK-CUR">
SEEK-CUR
</h3>
</a>
<div class="description">
external
</div>
<p class="sig">
Int
</p>
<span>
</span>
<p class="doc">
</p>
</div>
<div class="binder">
<a class="anchor" href="#SEEK-END">
<h3 id="SEEK-END">
SEEK-END
</h3>
</a>
<div class="description">
external
</div>
<p class="sig">
Int
</p>
<span>
</span>
<p class="doc">
</p>
</div>
<div class="binder">
<a class="anchor" href="#SEEK-SET">
<h3 id="SEEK-SET">
SEEK-SET
</h3>
</a>
<div class="description">
external
</div>
<p class="sig">
Int
</p>
<span>
</span>
<p class="doc">
</p>
</div>
<div class="binder">
<a class="anchor" href="#color">
<h3 id="color">
@ -238,6 +295,25 @@
closes a file pointer.
</p>
</div>
<div class="binder">
<a class="anchor" href="#fflush">
<h3 id="fflush">
fflush
</h3>
</a>
<div class="description">
external
</div>
<p class="sig">
(λ [(Ptr FILE)] ())
</p>
<span>
</span>
<p class="doc">
flushes a file pointer (i.e. commits every write).
</p>
</div>
<div class="binder">
<a class="anchor" href="#fgetc">
<h3 id="fgetc">
@ -276,6 +352,63 @@
opens a file by name using a mode (one or multiple of [r]ead, [w]rite, and [a]ppend), returns a file pointer.
</p>
</div>
<div class="binder">
<a class="anchor" href="#fread">
<h3 id="fread">
fread
</h3>
</a>
<div class="description">
external
</div>
<p class="sig">
(λ [a, Int, Int, (Ptr FILE)] Int)
</p>
<span>
</span>
<p class="doc">
reads from a file pointer into a pointer.
</p>
</div>
<div class="binder">
<a class="anchor" href="#fseek">
<h3 id="fseek">
fseek
</h3>
</a>
<div class="description">
external
</div>
<p class="sig">
(λ [(Ptr FILE), Int, Int] ())
</p>
<span>
</span>
<p class="doc">
sets the position indicator of a file.
</p>
</div>
<div class="binder">
<a class="anchor" href="#ftell">
<h3 id="ftell">
ftell
</h3>
</a>
<div class="description">
external
</div>
<p class="sig">
(λ [(Ptr FILE)] Int)
</p>
<span>
</span>
<p class="doc">
gets the position indicator of a file.
</p>
</div>
<div class="binder">
<a class="anchor" href="#fwrite">
<h3 id="fwrite">
@ -409,6 +542,44 @@
returns the contents of a file passed as argument as a string.
</p>
</div>
<div class="binder">
<a class="anchor" href="#rewind">
<h3 id="rewind">
rewind
</h3>
</a>
<div class="description">
external
</div>
<p class="sig">
(λ [(Ptr FILE)] ())
</p>
<span>
</span>
<p class="doc">
rewinds a file pointer (i.e. puts input and output stream to beginning).
</p>
</div>
<div class="binder">
<a class="anchor" href="#unlink">
<h3 id="unlink">
unlink
</h3>
</a>
<div class="description">
external
</div>
<p class="sig">
(λ [String] ())
</p>
<span>
</span>
<p class="doc">
unlinks a file (i.e. deletes it).
</p>
</div>
</div>
</body>
</html>

View File

@ -257,6 +257,25 @@
</p>
</div>
<div class="binder">
<a class="anchor" href="#nanotime">
<h3 id="nanotime">
nanotime
</h3>
</a>
<div class="description">
external
</div>
<p class="sig">
(λ [] Long)
</p>
<span>
</span>
<p class="doc">
Gets the current system time in nanoseconds as a long.
</p>
</div>
<div class="binder">
<a class="anchor" href="#signal">
<h3 id="signal">

View File

@ -154,7 +154,7 @@
(Float.random-between 10.0f 20.0f))
(defn print-random-floats []
(do (Random.seed 0.2)
(do (Random.seed-from 0.2)
(let [rands (Array.repeat 10 random-float)]
(println (ref (Array.str &rands))))))
@ -201,7 +201,7 @@
(defn main []
; here we always use the same seed for determinism, for simple random
; numbers that change you can for instance use (System.time)
(do (Random.seed 0.1)
(do (Random.seed-from 0.1)
(Things.call)
(use-doubles)
(println (ref (str (fib 10))))

View File

@ -39,7 +39,7 @@
(defn main []
(do (println "Seeding random number generator...\n")
(Random.seed (Double.from-int (System.time)))
(Random.seed-from (Double.from-int (System.time)))
(start-new-game!)
(while guessing
(let [user-input (get-line)

View File

@ -35,7 +35,7 @@
(do
; here we always use the same seed for determinism, for simple random
; numbers that change you can for instance use (System.time)
(Random.seed 0.1)
(Random.seed-from 0.1)
(let [monsters (Array.copy-map Monster.init-random &[@"Pegasus" @"Dragon" @"Devil"])]
(do
(println (ref (Array.str &monsters)))

View File

@ -11,6 +11,6 @@
Double.approx)
(assert-op test
0.536041
(do (Random.seed 33333.0) (Random.random))
(do (Random.seed-from 33333.0) (Random.random))
"deterministic randomization with seed works as expected"
Double.approx)))