1// Generated automatically from src/optional/wren_opt_random.wren. Do not edit.
2static const char* randomModuleSource =
3"foreign class Random {\n"
4" construct new() {\n"
5" seed_()\n"
6" }\n"
7"\n"
8" construct new(seed) {\n"
9" if (seed is Num) {\n"
10" seed_(seed)\n"
11" } else if (seed is Sequence) {\n"
12" if (seed.isEmpty) Fiber.abort(\"Sequence cannot be empty.\")\n"
13"\n"
14" // TODO: Empty sequence.\n"
15" var seeds = []\n"
16" for (element in seed) {\n"
17" if (!(element is Num)) Fiber.abort(\"Sequence elements must all be numbers.\")\n"
18"\n"
19" seeds.add(element)\n"
20" if (seeds.count == 16) break\n"
21" }\n"
22"\n"
23" // Cycle the values to fill in any missing slots.\n"
24" var i = 0\n"
25" while (seeds.count < 16) {\n"
26" seeds.add(seeds[i])\n"
27" i = i + 1\n"
28" }\n"
29"\n"
30" seed_(\n"
31" seeds[0], seeds[1], seeds[2], seeds[3],\n"
32" seeds[4], seeds[5], seeds[6], seeds[7],\n"
33" seeds[8], seeds[9], seeds[10], seeds[11],\n"
34" seeds[12], seeds[13], seeds[14], seeds[15])\n"
35" } else {\n"
36" Fiber.abort(\"Seed must be a number or a sequence of numbers.\")\n"
37" }\n"
38" }\n"
39"\n"
40" foreign seed_()\n"
41" foreign seed_(seed)\n"
42" foreign seed_(n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14, n15, n16)\n"
43"\n"
44" foreign float()\n"
45" float(end) { float() * end }\n"
46" float(start, end) { float() * (end - start) + start }\n"
47"\n"
48" foreign int()\n"
49" int(end) { (float() * end).floor }\n"
50" int(start, end) { (float() * (end - start)).floor + start }\n"
51"\n"
52" sample(list) {\n"
53" if (list.count == 0) Fiber.abort(\"Not enough elements to sample.\")\n"
54" return list[int(list.count)]\n"
55" }\n"
56" sample(list, count) {\n"
57" if (count > list.count) Fiber.abort(\"Not enough elements to sample.\")\n"
58"\n"
59" var result = []\n"
60"\n"
61" // The algorithm described in \"Programming pearls: a sample of brilliance\".\n"
62" // Use a hash map for sample sizes less than 1/4 of the population size and\n"
63" // an array of booleans for larger samples. This simple heuristic improves\n"
64" // performance for large sample sizes as well as reduces memory usage.\n"
65" if (count * 4 < list.count) {\n"
66" var picked = {}\n"
67" for (i in list.count - count...list.count) {\n"
68" var index = int(i + 1)\n"
69" if (picked.containsKey(index)) index = i\n"
70" picked[index] = true\n"
71" result.add(list[index])\n"
72" }\n"
73" } else {\n"
74" var picked = List.filled(list.count, false)\n"
75" for (i in list.count - count...list.count) {\n"
76" var index = int(i + 1)\n"
77" if (picked[index]) index = i\n"
78" picked[index] = true\n"
79" result.add(list[index])\n"
80" }\n"
81" }\n"
82"\n"
83" return result\n"
84" }\n"
85"\n"
86" shuffle(list) {\n"
87" if (list.isEmpty) return\n"
88"\n"
89" // Fisher-Yates shuffle.\n"
90" for (i in 0...list.count - 1) {\n"
91" var from = int(i, list.count)\n"
92" var temp = list[from]\n"
93" list[from] = list[i]\n"
94" list[i] = temp\n"
95" }\n"
96" }\n"
97"}\n";
98