random.alc 755 B

12345678910111213141516171819202122232425262728293031323334
  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. log("Return random " + cast_f2s(float_division(seed, m)))
  16. return float_division(seed, m)!
  17. Integer function random_interval(a : Integer, b : Integer):
  18. if (a == b):
  19. return a!
  20. else:
  21. return cast_f2i(random() * (b - a + 1) + a)!
  22. Element function random_choice(list : Element):
  23. if (list_len(list) == 0):
  24. return read_root()!
  25. else:
  26. return read_edge_dst(read_out(list, random_interval(0, read_nr_out(list) - 1)))!