lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>   I'm looking at the code, and I think that seeding the random number

> generator is *not* something it should do---that should be left to the

> user

> to do.

>

 

You are completely right. It's something I added, but was bugging me already.

 

>   -spc (What?  Not version 3 or 5 UUIDs? [4])

 

Nope. I needed a simple library, to generate the uuids randomly, no standards compliance, but pure Lua.

I'll make sure to add a note to the documentation.

 

 

But to make matters worse, the library currently creates the exact same sequence of uuids every time I start it.

 

The seed used is `socket.gettime()*10000` which is far better than `os.time()` __unless you notice__ that math.randomseed [1] casts the number to an int. Which means that the former gets reduced to either 0 or 1 (on my system) and all randomness is completely gone. (I only found out by accident)

 

Here's some test output, left to right;

1) uuid,

2) seed used (doubled every next iteration),

3) return value of math.randomseed (see below),

4) socket.gettime()*10000

 

C:\Users\Thijs\Dropbox\Lua projects\uuid>c:\users\public\lua\5.1\bin\lua.exe seedtest.lua

008cd403-87d6-42a0-c22f-29dcbc07559a    4       4       13682539533420

00dc02f2-c951-4345-c36e-24de700c0bb8    8       8       13682539533430

007b5ed2-4e47-468e-c6ee-1be1d81676f3    16      16      13682539533440

01bb1792-5731-4b20-cbee-08e6a72a4c6a    32      32      13682539533450

013a8a12-6a07-4544-c5ed-e3f14653f859    64      64      13682539533480

03386e12-8fb2-498c-c9eb-990783a45036    128     128     13682539533560

06353611-da09-421d-c2e7-0533fe4600f0    256     256     13682539533670

0d2dc610-6fb7-433f-c3de-de8bf48b6164    512     512     13682539533730

1a1fe70d-9912-4482-c4ce-8e3bdf14214d    1024    1024    13682539533910

34022808-eec7-4807-c8ae-ef9cb627a31d    2048    2048    13682539533960

68c9abfd-9733-4f13-cf6c-b05d644ca5bf    4096    4096    13682539534100

d156b0e7-e90a-4e2a-ceea-33dec097aa02    8192    8192    13682539534150

a270bbbb-8eb9-4b59-cbe6-39e1792bb588    16384   16384   13682539534210

44a4d163-d817-44b5-c4dd-45e7e955ca93    32768   32768   13682539534260

880cfdb3-6bd1-486f-c8cc-5df3c9a9f3aa    65536   65536   13682539534420

10dc5554-9147-4fe2-cfa9-8d0b895046d9    131072  131072  13682539534490

207c0595-dd32-4dc9-cd64-ed3b0a9ded36    262144  262144  13682539534580

40bc6418-7509-4896-c8d9-ae9a0b3939f0    524288  524288  13682539534650

7f3d221d-a6b7-4f30-cfc4-2e590e71d364    1048576 1048576 13682539534690

ff3e9f27-0812-4e64-ce99-2ed714e0054c    2097152 2097152 13682539534760

fe40983b-cbc8-4bcc-cb43-30d41fbe6a1c    4194304 4194304 13682539534860

fd458a64-5235-469d-c698-32cc357b35bd    8388608 8388608 13682539534890

fa4e6fb5-5f0e-4b3e-cb41-37bd62f4cafe    16777216        16777216        13682539534910

f4603957-79c0-4580-c593-419fbce6f480    33554432        33554432        13682539534940

e884cd9b-ad24-4904-c937-556370ca4884    67108864        67108864        13682539534960

d0ccf523-15ec-410c-c17f-7debd892f08c    134217728       134217728       13682539534980

a05c4533-e57c-411c-c10f-cdfba822409c    268435456       268435456       13682539535010

407ce553-859c-413c-c12f-6d1b4842e0bc    536870912       536870912       13682539535030

80bc2593-c5dc-417c-c16f-ad5b888220fc    1073741824      1073741824      13682539535050

003ca513-455c-41fc-c1ef-2ddb0802a07c    2147483648      -2147483648     13682539535070

003ca513-455c-41fc-c1ef-2ddb0802a07c    4294967296      0       13682539535100

 

Truncation (2nd and 3rd column unequal) occurs already at a far lower number (2,147,483,648) than the socket-seed created (13,682,539,535,070) and from there on randomness is gone (same uuids are generated).

 

Regarding return value of math.randomseed(); I temporarily modified Lua source to return the seed set;

static int math_randomseed (lua_State *L) {
  srand(luaL_checkint(L, 1));
  //return 0;
  lua_pushinteger(L, luaL_checkint(L, 1));  // return seed to make truncation visible
  return 1;
}

I looked math.randomseed() up in the manual [2] which only states: “Sets x as the "seed" for the pseudo-random generator: equal seeds produce equal sequences of numbers.”

 

I think a note would be worthwhile here, indicating that it the seed gets cast into a (system specific size) integer, and if the seed is outside the range of the system specific integer, the actual seed used is a system dependent value that probably has the opposite effect of what one is trying to achieve when setting the seed (lack of randomness when randomness is expected)

 

Possibly including the change above in math_randomseed in the Lua sources could be used to make it visible from the Lua side whether the seed was actually properly set in these cases.

 

Thijs

 

PS. Is there a way to detect the maximum int size used from Lua?

 

[1] http://www.lua.org/source/5.1/lmathlib.c.html#math_randomseed

[2] http://www.lua.org/manual/5.1/manual.html#pdf-math.randomseed