-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrandom.qs
More file actions
63 lines (54 loc) · 1.57 KB
/
Copy pathrandom.qs
File metadata and controls
63 lines (54 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# examples/random.qs — Random number generation examples
from "stdlib/random.qs" import seed, randint, randrange
from "stdlib/random.qs" import choice, shuffle, sample
# --- Basic random numbers ---
print("=== Random Numbers ===")
print(random()) # Float in [0.0, 1.0) - native builtin
print(randint(1, 6)) # Integer 1..6 (dice roll)
print(randrange(0, 10)) # Integer 0..9
# --- Deterministic seed ---
print("\n=== Deterministic Seed ===")
seed(42)
print(randint(1, 100)) # Always 82 with seed(42)
print(randint(1, 100)) # Always 27 with seed(42)
# Verify reproducibility
seed(42)
a = randint(1, 100)
seed(42)
b = randint(1, 100)
print("Reproducible: " + str(a == b))
# --- Choice ---
print("\n=== Choice ===")
colors = ["red", "green", "blue", "yellow"]
print("Random color: " + choice(colors))
# --- Shuffle ---
print("\n=== Shuffle ===")
cards = [1, 2, 3, 4, 5]
print("Before shuffle: " + str(cards))
shuffle(cards)
print("After shuffle: " + str(cards))
# Verify all elements preserved
shuffle(cards)
print("Still has all: " + str(len(cards) == 5))
# --- Sample ---
print("\n=== Sample ===")
pool = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
picked = sample(pool, 3)
print("Sampled 3: " + str(picked))
print("Original unchanged: " + str(len(pool) == 10))
# Verify no duplicates
seed(123)
picked2 = sample(pool, 5)
has_dupes = false
i = 0
while i < len(picked2) {
j = i + 1
while j < len(picked2) {
if picked2[i + 1] == picked2[j + 1] {
has_dupes = true
}
j = j + 1
}
i = i + 1
}
print("No duplicates: " + str(not has_dupes))