View Issue Details

IDProjectCategoryView StatusLast Update
0000323luatexluatex bugpublic2010-03-12 15:46
ReporterPawel Jackowski Assigned ToTaco  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionnot fixable 
Fixed in Version0.51.0 
Summary0000323: randomseed doesn't work in threads
DescriptionPlease try this code in generic lua and in luatex:

local rand = math.random
local seed = math.randomseed

seed(os.date("%S"))

co = coroutine.wrap(function()
  for i=1, 10 do print(rand()) end
end)
co()


In lua, I get a different sequence each compilation, as expected.
In luatex I get the same sequence of numbers each compilation.
The problem is no longer there if one use the loop with no coroutine wraper.
Additional InformationTried on trunk, rev. 3368
TagsNo tags attached.

Activities

Taco

2010-01-23 08:41

administrator   ~0000391

Are you running luatex or texlua? If luatex, can you provide the full input?

For

  texlua seed.lua

I do indeed get different results (rev 3368, linux-64)

Pawel Jackowski

2010-01-23 21:01

reporter   ~0000393

Last edited: 2010-01-26 09:29

I compared the result of calls:

lua seed.lua          (ok, different sequence on each run)
luatex -lua seed.lua  (not ok, the same sequence on each run)


But the same is observed if lua code included in tex, both \lua and \latelua modes:

%\latelua{
\lua{
  local rand = math.random
  local seed = math.randomseed

  seed(os.date("%S"))

  co = coroutine.wrap(function()
    for i=1, 10 do print(rand()) end
  end)
  co()
}\end


In all cases I always get the same sequence on numbers:

0.0012512588885159
0.56358531449324
0.19330423902097
0.80874050111393
0.58500930814539
0.47987304300058
0.3502914517655
0.89596240119633
0.82284005249184
0.7466048158208
...


rev 3374, vista 32.

Taco

2010-01-24 08:58

administrator   ~0000394

Weird. Are you using Akira's build or do you compile yourself?

(I assume that the bare "S" in the previous note is a typo?)

Pawel Jackowski

2010-01-25 14:28

reporter   ~0000395

Last edited: 2010-01-26 09:30

I use pure svn trunk build under MinGW. I'll try Akira build.

Yes, there should be 'seed(os.date("%S"))' instead of 'seed(os.date("S"))' in the previous note, sorry (corrected).

Pawel Jackowski

2010-01-26 10:17

reporter   ~0000396

Taco, the windows version of the binary shipped with TL has the same issue and gives *exactly* the same sequence of numbers each run. But the linux version works fine (tried on Suse 64bit). So this is some windowish crap, common for MinGW and VC environments. Not observed in normal lua, though.

Taco

2010-01-28 14:20

administrator   ~0000402

Can you check whether the problem is in math.randomseed() or in os.date(), please?

Pawel Jackowski

2010-01-29 21:00

reporter   ~0000403

It is math.randomseed() problem, os.date("%S") works as expected, see below.

As this is so weird, I've made all the tests once again, checking three
different windows binaries. To summarize what we know already:

0. The problem is: random numbers are constant, no matter what seed is used.
Happens ONLY if math.randomseed() is called OUTSIDE of coroutine function AND
math.random() is called INSIDE the coroutine, as in the example:

local rand = math.random
local seed = math.randomseed

--seed(1)
--seed(141421)
seed(314159)

co = coroutine.wrap(function()
    -- If you call seed here, everything is fine
    -- If you run this loop with no coroutine, everything is fine
    for i=1, 10 do print(rand()) end
end)
co()


1. No problem is observed in standard lua binary. Happens only with
luatex/texlua binaries for windows, Linux binary works just fine. First noticed
on mine executable built under MinGW, then confirmed on the one shipped with
TL. Now I also checked some other binaries from
http://minimals.contextgarden.net/setup/mswin/bin/ and the problem is there.

2. Applies to 'texlua seed.lua', 'luatex -lua seed.lua' as well as if the lua
code is wrapped by \directlua or \latelua primitives.

4. I just realized that the constant sequence of numbers I obtained in texlua
is the same as when I set math.randomseed(1) and run the code in normal lua.

If you have any idea what else I can check, I'll do wtih pleasure. If you would like to see all that stuff remotely, I'm for your disposal.

I'll try to find (or make) some other programs running lua from inside, meybe it is reproduceable not only in luatex.

Taco

2010-01-30 08:32

administrator   ~0000404

Apparently, each windows fiber (the threading model used by the coroutines on win32) resets the randomseed to 1. I am sorry, I have not a clue how to fix this.

Taco

2010-02-03 12:12

administrator   ~0000410

Hi Pawel,

Sorry, but this appears unfixable. Fibers simply don't inherit the state of the random generator of the main thread. I'll add a remark to the manual.

Issue History

Date Modified Username Field Change
2010-01-22 19:20 Pawel Jackowski New Issue
2010-01-23 08:41 Taco Note Added: 0000391
2010-01-23 08:41 Taco Status new => feedback
2010-01-23 21:01 Pawel Jackowski Note Added: 0000393
2010-01-24 08:58 Taco Note Added: 0000394
2010-01-25 14:28 Pawel Jackowski Note Added: 0000395
2010-01-26 09:29 Pawel Jackowski Note Edited: 0000393
2010-01-26 09:30 Pawel Jackowski Note Edited: 0000395
2010-01-26 10:17 Pawel Jackowski Note Added: 0000396
2010-01-28 14:20 Taco Note Added: 0000402
2010-01-29 21:00 Pawel Jackowski Note Added: 0000403
2010-01-30 08:32 Taco Note Added: 0000404
2010-01-30 08:32 Taco Status feedback => confirmed
2010-02-03 12:12 Taco Status confirmed => resolved
2010-02-03 12:12 Taco Resolution open => not fixable
2010-02-03 12:12 Taco Assigned To => Taco
2010-02-03 12:12 Taco Note Added: 0000410
2010-03-12 15:46 Taco Fixed in Version => 0.51.0
2010-03-12 15:46 Taco Status resolved => closed