lua-users home
lua-l archive

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


2015-11-19 20:49 GMT+02:00 Roberto Ierusalimschy <roberto@inf.puc-rio.br>:
>> Lua 5.3.2 (rc1) is now available for testing at
>>       http://www.lua.org/work/
>
> What is new:
>
> - table.sort "randomize" the pivot

This is done as follows:

static int choosePivot (int lo, int up) {
  unsigned int t = (unsigned int)(unsigned long)time(NULL);  /* time */
  unsigned int c = (unsigned int)(unsigned long)clock();  /* clock */
  unsigned int r4 = (unsigned int)(up - lo) / 4u;  /* range/4 */
  unsigned int p = (c + t) % (r4 * 2) + (lo + r4);
  lua_assert(lo + r4 <= p && p <= up - r4);
  return (int)p;
}

Since `time` returns a number of seconds, the value of `t` is quite
likely to be constant for any given invocation of `sort`.

Not very random.