1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- include "primitives.alh"
- include "utils.alh"
- Integer seed = 1
- Float function random():
- // Linear Congruential Generator
- Integer a
- Integer c
- Integer m
- // Parameters from Numerical Recipes
- a = 1664525
- c = 1013904223
- m = 4294967296
- // Do the generation and update the seed
- seed = integer_modulo(a * seed + c, m)
- // The seed is the new value
- return float_division(cast_float(seed), cast_float(m))!
- Integer function random_interval(a : Integer, b : Integer):
- if (a == b):
- return a!
- else:
- return cast_integer(random() * cast_float(b - a + 1) + cast_float(a))!
- Element function random_choice(list : Element):
- if (list_len(list) == 0):
- return read_root()!
- else:
- return list_read(list, random_interval(0, list_len(list) - 1))!
- String function random_string(length : Integer):
- String result
- Element chars
- chars = alphabet()
- result = ""
- while (string_len(result) < length):
- result = string_join(result, cast_string(random_choice(chars)))
- return result!
|