Curiosity about math.random in LuaTeX
Hi all, This is a curiosity question about the function math.random coming from Lua and used in mkiv: if I typeset the following test file in mkiv, no matter when nor how many times, the sequence of numbers which are output are the same… %%% begin test-random.tex \starttext \dorecurse{25}{% \ctxlua{tex.print(math.random(110,150))} \qquad \ctxlua{tex.print(math.random(math.random(110,120),math.random (140,150)))} \par} \stoptext %%% end test-random.tex Indeed I guess that this is due to the random seed used by LuaTeX: is it possible to force a new random seed upon each typesetting? Thanks for your attention: OK
Hi all, This is a curiosity question about the function math.random coming from Lua and used in mkiv: if I typeset the following test file in mkiv, no matter when nor how many times, the sequence of numbers which are output are the same… %%% begin test-random.tex \starttext \dorecurse{25}{% \ctxlua{tex.print(math.random(110,150))} \qquad \ctxlua{tex.print(math.random(math.random(110,120),math.random(140,150)))} \par} \stoptext %%% end test-random.tex Indeed I guess that this is due to the random seed used by LuaTeX: is it possible to force a new random seed upon each typesetting? Thanks for your attention: OK
Otared KAVIAN wrote:
Indeed I guess that this is due to the random seed used by LuaTeX: is it possible to force a new random seed upon each typesetting?
The randomseed is set by context, and saved between consecutive runs of the same file. Normally, this is what you would want: just imagine what happens if each of the luatex runs in a 'context' call used a different value always. However (this is mostly for Hans), there is a bug in that process: all randomseeds in mkiv appear to be between 0 and 1. Since math.randomseed() expects an integer, this gives almost no randomness at all to the seed values. If you want near-random randomness, you can set the seed yourself based on the return value of os.time(): \ctxlua{math.randomseed(os.time())} Best wishes, Taco
On Oct 25, 2009, at 9:26 AM, Taco Hoekwater wrote:
Otared KAVIAN wrote:
Indeed I guess that this is due to the random seed used by LuaTeX: is it possible to force a new random seed upon each typesetting?
The randomseed is set by context, and saved between consecutive runs of the same file. Normally, this is what you would want: just imagine what happens if each of the luatex runs in a 'context' call used a different value always.
However (this is mostly for Hans), there is a bug in that process: all randomseeds in mkiv appear to be between 0 and 1. Since math.randomseed() expects an integer, this gives almost no randomness at all to the seed values.
If you want near-random randomness, you can set the seed yourself based on the return value of os.time():
\ctxlua{math.randomseed(os.time())}
Ah, that explains why I could never get random numbers in mkiv when I tried a year ago. Another suggestion: there's also a \getrandomnumber command available in ConTeXt, which seems to work well. Thomas
Thomas and Taco: thanks to both of you for your insight. I overlooked the function math.randomseed() in Lua, and my question was mainly out of curioisty: in practive one doesn't need to reset the random seed, as Taco points out. However I couldn't find the right usage of the command « \getrandomnumber »… Best regards: OK On 25 oct. 2009, at 10:50, Thomas A. Schmitz wrote:
On Oct 25, 2009, at 9:26 AM, Taco Hoekwater wrote:
Otared KAVIAN wrote:
Indeed I guess that this is due to the random seed used by LuaTeX: is it possible to force a new random seed upon each typesetting?
The randomseed is set by context, and saved between consecutive runs of the same file. Normally, this is what you would want: just imagine what happens if each of the luatex runs in a 'context' call used a different value always.
However (this is mostly for Hans), there is a bug in that process: all randomseeds in mkiv appear to be between 0 and 1. Since math.randomseed() expects an integer, this gives almost no randomness at all to the seed values.
If you want near-random randomness, you can set the seed yourself based on the return value of os.time():
\ctxlua{math.randomseed(os.time())}
Ah, that explains why I could never get random numbers in mkiv when I tried a year ago. Another suggestion: there's also a \getrandomnumber command available in ConTeXt, which seems to work well.
Thomas ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________
participants (4)
-
Otared Kavian
-
Otared KAVIAN
-
Taco Hoekwater
-
Thomas A. Schmitz