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 | |
parent | 3c0d3c6fbeea18f257102c62a01b036c7a5c5161 (diff) | |
download | lua-2f22c6bb79d209a55b3fc8e0b2d9c9f89f038174.tar.gz lua-2f22c6bb79d209a55b3fc8e0b2d9c9f89f038174.tar.bz2 lua-2f22c6bb79d209a55b3fc8e0b2d9c9f89f038174.zip |
'math.randomseed' always returns the two seed components
-rw-r--r-- | lmathlib.c | 14 | ||||
-rw-r--r-- | manual/manual.of | 6 | ||||
-rw-r--r-- | testes/math.lua | 6 |
3 files changed, 15 insertions, 11 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 | ||
diff --git a/manual/manual.of b/manual/manual.of index 7f2596fa..1646f113 100644 --- a/manual/manual.of +++ b/manual/manual.of | |||
@@ -7798,8 +7798,10 @@ The default for @id{y} is zero. | |||
7798 | When called with no arguments, | 7798 | When called with no arguments, |
7799 | Lua generates a seed with | 7799 | Lua generates a seed with |
7800 | a weak attempt for randomness. | 7800 | a weak attempt for randomness. |
7801 | In this case, | 7801 | |
7802 | the call returns the two seed components that were used. | 7802 | This function returns the two seed components |
7803 | that were effectively used, | ||
7804 | so that setting them again repeats the sequence. | ||
7803 | 7805 | ||
7804 | To ensure a required level of randomness to the initial state | 7806 | To ensure a required level of randomness to the initial state |
7805 | (or contrarily, to have a deterministic sequence, | 7807 | (or contrarily, to have a deterministic sequence, |
diff --git a/testes/math.lua b/testes/math.lua index 0c297e74..d0aaa6a5 100644 --- a/testes/math.lua +++ b/testes/math.lua | |||
@@ -842,9 +842,11 @@ end | |||
842 | 842 | ||
843 | do | 843 | do |
844 | -- testing return of 'randomseed' | 844 | -- testing return of 'randomseed' |
845 | local <const> x, <const> y = math.randomseed() | 845 | local x, y = math.randomseed() |
846 | local res = math.random(0) | 846 | local res = math.random(0) |
847 | math.randomseed(x, y) -- should repeat the state | 847 | x, y = math.randomseed(x, y) -- should repeat the state |
848 | assert(math.random(0) == res) | ||
849 | math.randomseed(x, y) -- again should repeat the state | ||
848 | assert(math.random(0) == res) | 850 | assert(math.random(0) == res) |
849 | -- keep the random seed for following tests | 851 | -- keep the random seed for following tests |
850 | end | 852 | end |