random.alc 696 B

123456789101112131415161718192021222324252627282930313233
  1. include "primitives.alh"
  2. Integer seed = 1
  3. Float function random():
  4. // Linear Congruential Generator
  5. Integer a
  6. Integer c
  7. Integer m
  8. // Parameters from Numerical Recipes
  9. a = 1664525
  10. c = 1013904223
  11. m = 4294967296
  12. // Do the generation and update the seed
  13. seed = integer_modulo(a * seed + c, m)
  14. // The seed is the new value
  15. return float_division(seed, m)!
  16. Integer function random_interval(a : Integer, b : Integer):
  17. if (a == b):
  18. return a!
  19. else:
  20. return cast_f2i(random() * (b - a + 1) + a)!
  21. Element function random_choice(list : Element):
  22. if (list_len(list) == 0):
  23. return read_root()!
  24. else:
  25. return read_edge_dst(read_out(list, random_interval(0, read_nr_out(list) - 1)))!