(* random.sml *) (* Copyright (C) 2007 Alley Stoughton This file is part of crypto, a cryptogram encoder/decoder. See the file COPYING.txt for copying and usage restrictions *) (* a structure for generating random numbers *) (* will only compile if IntInf = LargeInt *) structure Random :> RANDOM = struct (* currently, Word31.word *) type seed = Rand.rand fun makeFromTime() = let val secs : IntInf.int = Time.toSeconds(Time.now()) val randMin = Word31.toLargeInt Rand.randMin val randMax = Word31.toLargeInt Rand.randMax in Word31.fromLargeInt(randMin + secs mod (randMax - randMin + 1)) end exception BadRange fun getInRange(seed, m, n) = if m > n then raise BadRange else let val seed = Rand.random seed val m : IntInf.int = LargeInt.fromInt m val n = LargeInt.fromInt n val i = Word31.toLargeInt seed mod (n - m + 1) + m in (seed, LargeInt.toInt i) end end;