From 4eefef07ab1c136f901d816822c79336fa89336d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 17 Jul 2019 16:00:24 -0300 Subject: 'math.randomseed()' returns the seeds it used A call to 'math.randomseed()' returns the two components of the seed it set, so that they can be used to set that same seed again. --- testes/all.lua | 11 ++++++++--- testes/math.lua | 11 +++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) (limited to 'testes') diff --git a/testes/all.lua b/testes/all.lua index 72121e8d..bf27f106 100644 --- a/testes/all.lua +++ b/testes/all.lua @@ -37,8 +37,6 @@ end -- tests should require debug when needed debug = nil -require"bwcoercion" - if usertests then T = nil -- no "internal" tests for user tests @@ -46,7 +44,6 @@ else T = rawget(_G, "T") -- avoid problems with 'strict' module end -math.randomseed(0) --[=[ example of a long [comment], @@ -54,6 +51,14 @@ math.randomseed(0) ]=] +print("\n\tStarting Tests") + +do + -- set random seed + local random_x, random_y = math.randomseed() + print(string.format("random seeds: %d, %d", random_x, random_y)) +end + print("current path:\n****" .. package.path .. "****\n") diff --git a/testes/math.lua b/testes/math.lua index befce12e..0c297e74 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -815,7 +815,7 @@ end -- low-level!! For the current implementation of random in Lua, -- the first call after seed 1007 should return 0x7a7040a5a323c9d6 do - -- all computations assume at most 32-bit integers + -- all computations should work with 32-bit integers local h = 0x7a7040a5 -- higher half local l = 0xa323c9d6 -- lower half @@ -840,7 +840,14 @@ do assert(rand * 2^floatbits == res) end -math.randomseed() +do + -- testing return of 'randomseed' + local x, y = math.randomseed() + local res = math.random(0) + math.randomseed(x, y) -- should repeat the state + assert(math.random(0) == res) + -- keep the random seed for following tests +end do -- test random for floats local randbits = math.min(floatbits, 64) -- at most 64 random bits -- cgit v1.2.3-55-g6feb