random.alc 583 B

123456789101112131415161718192021222324252627
  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 seed / m
  16. Integer function random_interval(a : Integer, b : Integer):
  17. return cast_f2i(random() * (b - a) + a)
  18. Element function random_choice(list : Element):
  19. return read_edge_dst(read_out(list, random_interval(0, read_nr_out(list) - 1)))