Heyas,
It seems that during LuaIntro, math.random() is not seeded yet so math.random() always returns the same number. Interestingly, os.time() doesn't seem to always be very reliable. It sometimes goes back to a number it already returned, and it never returns an odd number.
But I'd need some sort of a (pseudo)random number generator to show a random tip or unit description in the loading screen. I was thinking on doing something as silly as keeping count of what tip/description we last showed in a file and then increment each time, but that just.. doesn't feel elegant at all.
So might there be any other way to get an incremental or pseudorandom number during LuaIntro?
LuaIntro and random numbers
Moderator: Moderators
Re: LuaIntro and random numbers
Courtesy to Doo for the snippet, went with a counter number stored in a file, roughly like so:
Code: Select all
-- Random tips we can show
local tips = {"Tip #294:\n\nHave trouble finding metal spots?\nPress F4 to switch to the metal map.",
"Tip #15: Some other tip."}
-- Random unit descriptions we can show
local unit_descs = {"armacv.dds ARM Amphibious Constructor\nDescription comes here after the image. Image must end to dds. Image must be in unitpics",
"armamb.dds Some other unit."}
-- Since math.random is not random and always the same, we save a counter to a file and use that.
filename = "LuaUI/Config/randomseed.data"
k = os.time() % 1500
if VFS.FileExists(filename) then
local file = assert(io.open(filename,'r'), "Unable to load latest randomseed from "..filename)
k = math.floor(tonumber(file:read())) % 1500
file:close()
end
k = k + 1
local file = assert(io.open(filename,'w'), "Unable to save latest randomseed from "..filename)
file:write(k)
file:close()
file = nil
local random_tip_or_desc = unit_descs[((k/2) % #unit_descs) + 1]
if k%2 == 1 then
random_tip_or_desc = tips[((math.ceil(k/2)) % #tips) + 1]
end
Re: LuaIntro and random numbers
Personally I simply use that:
ran once at the initialisation of my LuaIntro Addon
You'll notice I don't mess the random generator itself, but call it a variable number of time. This way I don't risk making it less random. I still use math.random() randomness, and its default seed, for example if it gets fixed I'll benefit from it. Or if os.time() is too predictable, well I'm using it only indirectly so it won't show.
Also, when I was toying with math.random in Gadgets, I found experimentally that sometimes the first few random numbers are the same, and only after a few cycles do random number appears, hence why I set a minimum number of discarded draws.
Code: Select all
-- Betterize Random
for k=1,80+os.time()%200 do
math.random()
end
You'll notice I don't mess the random generator itself, but call it a variable number of time. This way I don't risk making it less random. I still use math.random() randomness, and its default seed, for example if it gets fixed I'll benefit from it. Or if os.time() is too predictable, well I'm using it only indirectly so it won't show.
Also, when I was toying with math.random in Gadgets, I found experimentally that sometimes the first few random numbers are the same, and only after a few cycles do random number appears, hence why I set a minimum number of discarded draws.
Re: LuaIntro and random numbers
Wouldn't this create desyncs?
- Silentwings
- Posts: 3720
- Joined: 25 Oct 2008, 00:23
Re: LuaIntro and random numbers
No, because LuaIntro is unsynced code.
Re: LuaIntro and random numbers
Not quite true, the basecontent LuaIntro/config.lua does contain a math.randomseed(os.gettime()) call that was added to fix https://springrts.com/mantis/view.php?id=3962 (though it actually seeds all unsynced Lua states) prior to commit 3ec84a4b. BA's (custom?) copy uses os.clock() as its input, which is about as bad.It seems that during LuaIntro, math.random() is not seeded yet
The tricks above aren't needed in 105 or the next maintenance build which will handle preseeding for you.