LuaIntro and random numbers

LuaIntro and random numbers

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
tzaeru
Posts: 283
Joined: 28 Oct 2007, 02:23

LuaIntro and random numbers

Post by tzaeru »

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?
tzaeru
Posts: 283
Joined: 28 Oct 2007, 02:23

Re: LuaIntro and random numbers

Post by tzaeru »

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
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7049
Joined: 16 Nov 2004, 13:08

Re: LuaIntro and random numbers

Post by zwzsg »

Personally I simply use that:

Code: Select all

	-- Betterize Random
	for k=1,80+os.time()%200 do
		math.random()
	end
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.
User avatar
Jools
XTA Developer
Posts: 2816
Joined: 23 Feb 2009, 16:29

Re: LuaIntro and random numbers

Post by Jools »

Wouldn't this create desyncs?
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

Re: LuaIntro and random numbers

Post by Silentwings »

No, because LuaIntro is unsynced code.
Kloot
Spring Developer
Posts: 1867
Joined: 08 Oct 2006, 16:58

Re: LuaIntro and random numbers

Post by Kloot »

It seems that during LuaIntro, math.random() is not seeded yet
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.

The tricks above aren't needed in 105 or the next maintenance build which will handle preseeding for you.
Post Reply

Return to “Lua Scripts”