[Lua] RandomSeed and MatchID

edited February 2017 in Support

Just spent some time working on this, so I thought I'd share it.

-- Change random seed
-- Tested this with numbers in the range of 132517192800 which is above the theoretical max value.
-- Theoretical max is 1231yy235959 where yy is the year. So this seed should be an ever changing safe value.
local dateTimeString = string.gsub(string.gsub(GetSystemDate()..GetSystemTime(), '%W', ''), '^0+','')

-- Grab the MatchID, or fake it in tools mode.
if IsInToolsMode() then
    -- If we are in tools mode, there is no worry about a duplicate match from concurrent start time.
    GameMode.MatchID = dateTimeString
    -- If we are live, then Valve handles the match ID value.
    GameMode.MatchID = tostring(GameRules:GetMatchID())

I believe this to be a slight improvement over the somewhat standard practice:


  -- Change random seed
  local timeTxt = string.gsub(string.gsub(GetSystemTime(), ':', ''), '^0+','')

Because depending on your needs for the random values, the above value will create identical random values every day for any games that start on the same second as some other day.

For a majority of users, this is probably not an issue, but I like to be random.

Despite the potential problem of large numeric values being truncated, I did test with a very large value, and incremented/decremented and found the random sequences were indeed changing reasonably.

Also, if you need a match id number to change on your local development, that number is great for that too. :)

Edit: Changed to read "ever changing" since it is not "ever increasing", since it will jump way back when the year changes. But there will be no duplicates.