include "primitives.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
	log("Return random " + cast_f2s(float_division(seed, m)))
	return float_division(seed, m)

Integer function random_interval(a : Integer, b : Integer):
	if (a == b):
		return a
	else:
		return cast_f2i(random() * (b - a + 1) + a)

Element function random_choice(list : Element):
	if (list_len(list) == 0):
		return read_root()
	else:
		return read_edge_dst(read_out(list, random_interval(0, read_nr_out(list) - 1)))
