[MLton] Wrong behaviour of MLton.Random.alphaNumString?
Jesper Louis Andersen
jlouis@mongers.org
Sun, 28 Dec 2003 22:38:19 +0100
I have the follwing little code snippet:
structure GenRandomFile =
struct
(* Generate a n lines of random data, outputting it to file fname *)
fun genData (n,fname) =
let val ostream = TextIO.openOut fname
fun loop 0 = TextIO.closeOut ostream
| loop n =
( TextIO.output (ostream,
String.concat [
MLton.Random.alphaNumString 76,
"\n"
]
)
; loop (n-1)
)
in
(case n of
NONE => loop 1000
| SOME n => loop n
)
end
val _ =
case CommandLine.arguments () of
[fname, n] => ( genData (Int.fromString n, fname)
; OS.Process.success
)
| _ => OS.Process.failure
end
Which could be cleaner, nevermind that. running this with:
annah$ uname -a
NetBSD annah 1.6ZG NetBSD 1.6ZG (ANNAH) #0: Fri Dec 12 21:07:23 CET 2003 root@annah:/usr/src/sys/arch/i386/compile/ANNAH i386
annah$ mlton
MLton MLTONVERSION (built Sun Dec 14 02:41:24 2003 on annah)
./create-data test 10 gives the following output:
ELADkXx8Qb100000000000000000000000000000000000000000000000000000000000000000
efoj0jKp30300000000000000000000000000000000000000000000000000000000000000000
qnMH47Sq1O200000000000000000000000000000000000000000000000000000000000000000
KbknWRft8q000000000000000000000000000000000000000000000000000000000000000000
sFKXmJc57k000000000000000000000000000000000000000000000000000000000000000000
0zM3eDupVu200000000000000000000000000000000000000000000000000000000000000000
2X2RIZUO53300000000000000000000000000000000000000000000000000000000000000000
Ot8BcJjHSM000000000000000000000000000000000000000000000000000000000000000000
eLcv43onRr200000000000000000000000000000000000000000000000000000000000000000
eza7a7Xdj5400000000000000000000000000000000000000000000000000000000000000000
This is not very random to me. So:
a) Is this the intended behaviour of alphaNumString? If yes I think
we should explicitly note it only works up to a certain size
b) If it is not the intended behaviour, i would like to have input
from other architectures than NetBSD, in order to narrow the bug down.
c) I tried to take a look at the code in question
(lib/mlton-stubs/random.sml). I find it rather wierd that after the
first 6 characters have been set no more calls to rand () occurs.
(Line 19 to 46). It seems that the random function works by wrapping
around the word, and currently I do not have the time to read the
algebra needed to get that little bugger sorted out.
--
j.