Added new example that shows the usage of random_int() and random_seed(). Related parser fixes. (0.37.6)
This commit is contained in:
parent
40584f27f5
commit
4039abd10d
4 changed files with 91 additions and 9 deletions
|
|
@ -45,9 +45,9 @@ print(max(5, 9)) // -> 9
|
|||
print(clamp(15, 0, 10)) // -> 10
|
||||
print(abs(-7)) // -> 7
|
||||
print(pow(2, 8)) // -> 256
|
||||
random(123) // seed RNG
|
||||
print(randomInt(0, 3)) // -> 0..2 (deterministic for seed 123)
|
||||
print(randomInt(5, 6)) // -> 5
|
||||
random_seed(123) // seed RNG
|
||||
print(random_int(0, 3)) // -> 0..2 (deterministic for seed 123)
|
||||
print(random_int(5, 6)) // -> 5
|
||||
|
||||
/* Expected output:
|
||||
a-b-c
|
||||
|
|
|
|||
81
examples/random_demo.fun
Normal file
81
examples/random_demo.fun
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-12-11
|
||||
*/
|
||||
|
||||
// Demonstrates usage of RANDOM_SEED and RANDOM_INT opcodes via
|
||||
// the built-ins: random_seed(seed) and random_int(lo, hiExclusive).
|
||||
|
||||
print("-- Random demo: seed reproducibility and bounds --")
|
||||
|
||||
// Seed with a fixed value and produce a short sequence
|
||||
seed = 123456
|
||||
random_seed(seed)
|
||||
a1 = random_int(0, 10) // in [0,10)
|
||||
a2 = random_int(0, 10)
|
||||
a3 = random_int(5, 8) // in [5,8)
|
||||
|
||||
print("First run:")
|
||||
print(a1)
|
||||
print(a2)
|
||||
print(a3)
|
||||
|
||||
// Re-seed with the same value: the sequence should repeat
|
||||
random_seed(seed)
|
||||
b1 = random_int(0, 10)
|
||||
b2 = random_int(0, 10)
|
||||
b3 = random_int(5, 8)
|
||||
|
||||
print("Second run (after re-seed):")
|
||||
print(b1)
|
||||
print(b2)
|
||||
print(b3)
|
||||
|
||||
print("Reproducible? (a1==b1, a2==b2, a3==b3)")
|
||||
print(a1 == b1)
|
||||
print(a2 == b2)
|
||||
print(a3 == b3)
|
||||
|
||||
// Show that the upper bound is exclusive by sampling multiple times
|
||||
// and tracking the maximum seen value; it should never reach hi.
|
||||
lo = 10
|
||||
hi = 20
|
||||
max_seen = lo
|
||||
i = 0
|
||||
while i < 100
|
||||
v = random_int(lo, hi)
|
||||
if (v > max_seen) max_seen = v
|
||||
i = i + 1
|
||||
|
||||
print("Max seen in [" + to_string(lo) + "," + to_string(hi) + ") over 100 samples:")
|
||||
print(max_seen)
|
||||
print("Is max_seen < hi? ")
|
||||
print(max_seen < hi)
|
||||
|
||||
/* Expected output:
|
||||
-- Random demo: seed reproducibility and bounds --
|
||||
First run:
|
||||
9
|
||||
3
|
||||
5
|
||||
Second run (after re-seed):
|
||||
9
|
||||
3
|
||||
5
|
||||
Reproducible? (a1==b1, a2==b2, a3==b3)
|
||||
true
|
||||
true
|
||||
true
|
||||
Max seen in [10,20) over 100 samples:
|
||||
19
|
||||
Is max_seen < hi?
|
||||
1
|
||||
*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue