diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-03-20 16:13:17 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-03-20 16:13:17 -0300 |
| commit | 5a04f1851e0d42b4bcbb0af103490bc964e985aa (patch) | |
| tree | 227f98d4fff3f1bd1eea4d20ddd4aa0ec7562ddd /ltablib.c | |
| parent | 8c064fdc23bd745bbd3456a58cc9e2521f8e4263 (diff) | |
| download | lua-5a04f1851e0d42b4bcbb0af103490bc964e985aa.tar.gz lua-5a04f1851e0d42b4bcbb0af103490bc964e985aa.tar.bz2 lua-5a04f1851e0d42b4bcbb0af103490bc964e985aa.zip | |
New function 'luaL_makeseed'
This function unifies code from 'lua_newstate', 'math.randomseed',
and 'table.sort' that tries to create a value with a minimum level
of randomness.
Diffstat (limited to 'ltablib.c')
| -rw-r--r-- | ltablib.c | 29 |
1 files changed, 3 insertions, 26 deletions
| @@ -230,31 +230,8 @@ typedef unsigned int IdxT; | |||
| 230 | ** of a partition. (If you don't want/need this "randomness", ~0 is a | 230 | ** of a partition. (If you don't want/need this "randomness", ~0 is a |
| 231 | ** good choice.) | 231 | ** good choice.) |
| 232 | */ | 232 | */ |
| 233 | #if !defined(l_randomizePivot) /* { */ | 233 | #if !defined(l_randomizePivot) |
| 234 | 234 | #define l_randomizePivot(L) luaL_makeseed(L) | |
| 235 | #include <time.h> | ||
| 236 | |||
| 237 | /* size of 'e' measured in number of 'unsigned int's */ | ||
| 238 | #define sof(e) (sizeof(e) / sizeof(unsigned int)) | ||
| 239 | |||
| 240 | /* | ||
| 241 | ** Use 'time' and 'clock' as sources of "randomness". Because we don't | ||
| 242 | ** know the types 'clock_t' and 'time_t', we cannot cast them to | ||
| 243 | ** anything without risking overflows. A safe way to use their values | ||
| 244 | ** is to copy them to an array of a known type and use the array values. | ||
| 245 | */ | ||
| 246 | static unsigned int l_randomizePivot (void) { | ||
| 247 | clock_t c = clock(); | ||
| 248 | time_t t = time(NULL); | ||
| 249 | unsigned int buff[sof(c) + sof(t)]; | ||
| 250 | unsigned int i, rnd = 0; | ||
| 251 | memcpy(buff, &c, sof(c) * sizeof(unsigned int)); | ||
| 252 | memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int)); | ||
| 253 | for (i = 0; i < sof(buff); i++) | ||
| 254 | rnd += buff[i]; | ||
| 255 | return rnd; | ||
| 256 | } | ||
| 257 | |||
| 258 | #endif /* } */ | 235 | #endif /* } */ |
| 259 | 236 | ||
| 260 | 237 | ||
| @@ -391,7 +368,7 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up, | |||
| 391 | up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */ | 368 | up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */ |
| 392 | } | 369 | } |
| 393 | if ((up - lo) / 128 > n) /* partition too imbalanced? */ | 370 | if ((up - lo) / 128 > n) /* partition too imbalanced? */ |
| 394 | rnd = l_randomizePivot(); /* try a new randomization */ | 371 | rnd = l_randomizePivot(L); /* try a new randomization */ |
| 395 | } /* tail call auxsort(L, lo, up, rnd) */ | 372 | } /* tail call auxsort(L, lo, up, rnd) */ |
| 396 | } | 373 | } |
| 397 | 374 | ||
