diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-07-19 13:31:53 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-07-19 13:31:53 -0300 |
| commit | 2f22c6bb79d209a55b3fc8e0b2d9c9f89f038174 (patch) | |
| tree | 9fcece0aa1fa65f52b547c51e3c39181815f0887 /lmathlib.c | |
| parent | 3c0d3c6fbeea18f257102c62a01b036c7a5c5161 (diff) | |
| download | lua-2f22c6bb79d209a55b3fc8e0b2d9c9f89f038174.tar.gz lua-2f22c6bb79d209a55b3fc8e0b2d9c9f89f038174.tar.bz2 lua-2f22c6bb79d209a55b3fc8e0b2d9c9f89f038174.zip | |
'math.randomseed' always returns the two seed components
Diffstat (limited to 'lmathlib.c')
| -rw-r--r-- | lmathlib.c | 14 |
1 files changed, 7 insertions, 7 deletions
| @@ -586,7 +586,8 @@ static int math_random (lua_State *L) { | |||
| 586 | } | 586 | } |
| 587 | 587 | ||
| 588 | 588 | ||
| 589 | static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) { | 589 | static void setseed (lua_State *L, Rand64 *state, |
| 590 | lua_Unsigned n1, lua_Unsigned n2) { | ||
| 590 | int i; | 591 | int i; |
| 591 | state[0] = Int2I(n1); | 592 | state[0] = Int2I(n1); |
| 592 | state[1] = Int2I(0xff); /* avoid a zero state */ | 593 | state[1] = Int2I(0xff); /* avoid a zero state */ |
| @@ -594,6 +595,8 @@ static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) { | |||
| 594 | state[3] = Int2I(0); | 595 | state[3] = Int2I(0); |
| 595 | for (i = 0; i < 16; i++) | 596 | for (i = 0; i < 16; i++) |
| 596 | nextrand(state); /* discard initial values to "spread" seed */ | 597 | nextrand(state); /* discard initial values to "spread" seed */ |
| 598 | lua_pushinteger(L, n1); | ||
| 599 | lua_pushinteger(L, n2); | ||
| 597 | } | 600 | } |
| 598 | 601 | ||
| 599 | 602 | ||
| @@ -605,9 +608,7 @@ static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) { | |||
| 605 | static void randseed (lua_State *L, RanState *state) { | 608 | static void randseed (lua_State *L, RanState *state) { |
| 606 | lua_Unsigned seed1 = (lua_Unsigned)time(NULL); | 609 | lua_Unsigned seed1 = (lua_Unsigned)time(NULL); |
| 607 | lua_Unsigned seed2 = (lua_Unsigned)(size_t)L; | 610 | lua_Unsigned seed2 = (lua_Unsigned)(size_t)L; |
| 608 | lua_pushinteger(L, seed1); | 611 | setseed(L, state->s, seed1, seed2); |
| 609 | lua_pushinteger(L, seed2); | ||
| 610 | setseed(state->s, seed1, seed2); | ||
| 611 | } | 612 | } |
| 612 | 613 | ||
| 613 | 614 | ||
| @@ -615,14 +616,13 @@ static int math_randomseed (lua_State *L) { | |||
| 615 | RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); | 616 | RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); |
| 616 | if (lua_isnone(L, 1)) { | 617 | if (lua_isnone(L, 1)) { |
| 617 | randseed(L, state); | 618 | randseed(L, state); |
| 618 | return 2; /* return seeds */ | ||
| 619 | } | 619 | } |
| 620 | else { | 620 | else { |
| 621 | lua_Integer n1 = luaL_checkinteger(L, 1); | 621 | lua_Integer n1 = luaL_checkinteger(L, 1); |
| 622 | lua_Integer n2 = luaL_optinteger(L, 2, 0); | 622 | lua_Integer n2 = luaL_optinteger(L, 2, 0); |
| 623 | setseed(state->s, n1, n2); | 623 | setseed(L, state->s, n1, n2); |
| 624 | return 0; | ||
| 625 | } | 624 | } |
| 625 | return 2; /* return seeds */ | ||
| 626 | } | 626 | } |
| 627 | 627 | ||
| 628 | 628 | ||
