AKA_Clutter Posted February 18, 2023 Posted February 18, 2023 (edited) Hi, I want to create some randomness in my missions via scripts. I know that you can use math.random () lua function. I asl oknow that you can use math.randomseed() to set the seed for the random number generator. I also know that in lua I can use math.random(os.time()). However, I have tried using this inside of a script running in DCS and it gives me an erro stating something about trying to concatenate os with a nil value. Anyone know why I might be getting this, and what is a good seed to use for the RNG? Thanks --[[ ========================================================== Define functions to deterime random time ========================================================== --]] math.randomseed(os.time()) local function Random_Time_Window_10 () RTW_10 = math.random (0,10) return (RTW) end Edited February 18, 2023 by AKA_Clutter Add information ---------------- AKA_Clutter Win 10 Pro, Intel i7 12700k @4.6 GHz, EVGA RTX 3080 FTW, Ultra 64 GB G.Skill DDR4 3600 RAM, Acer 27" flat screen, HP Reverb G2, TM Warthog HOTAS with Virpil warBRD base, MFG Rudder Pedals, Virpil TCS Rotor Base with AH-64Dcollective, TrackIR 5 Pro w/Vector Expansion, PointCTRL.
TEMPEST.114 Posted February 18, 2023 Posted February 18, 2023 (edited) 1. In lua, new variables have to have local in front of them unless they are globals - but unless RTW_10 is a global defined outside of this function, then it's scope is limited to that function. 2. You're returning a variable called 'RTW' but the variable that has the random number is in is 'RTW_10'. Where is RTW coming from? They have to be the same variable. 3. Have you de-sanitised DCS? If not, you won't have access to OS level commands. Edited February 18, 2023 by Elphaba
AKA_Clutter Posted February 18, 2023 Author Posted February 18, 2023 It may be the desensitized part. Thanks. ---------------- AKA_Clutter Win 10 Pro, Intel i7 12700k @4.6 GHz, EVGA RTX 3080 FTW, Ultra 64 GB G.Skill DDR4 3600 RAM, Acer 27" flat screen, HP Reverb G2, TM Warthog HOTAS with Virpil warBRD base, MFG Rudder Pedals, Virpil TCS Rotor Base with AH-64Dcollective, TrackIR 5 Pro w/Vector Expansion, PointCTRL.
AKA_Clutter Posted February 18, 2023 Author Posted February 18, 2023 (edited) Humm, that doesn't seem to be it. I commented out the three sanitize lines and still get the same error. I use the call in a non-dcs lua file and it works as advertised. Any other guesses? Edited February 18, 2023 by AKA_Clutter ---------------- AKA_Clutter Win 10 Pro, Intel i7 12700k @4.6 GHz, EVGA RTX 3080 FTW, Ultra 64 GB G.Skill DDR4 3600 RAM, Acer 27" flat screen, HP Reverb G2, TM Warthog HOTAS with Virpil warBRD base, MFG Rudder Pedals, Virpil TCS Rotor Base with AH-64Dcollective, TrackIR 5 Pro w/Vector Expansion, PointCTRL.
cfrag Posted February 19, 2023 Posted February 19, 2023 (edited) Since you are accessing "os" (by using os.time()) and os is sanitized (set to nil in a standard DCS installation) I believe the root cause is the same: os is still sanitized. That being said, of the three libraries that can be desanitized (lfs, io and os), os is definitively the one I recommend you should NOT desanitize, it is far too dangerous to leave open (os allows spawning new processes). All that being said, you can set up a very simple script to test if you have correctly de-sanitized "os" in your DCS installation (remember that you must fully quit and then restart DCS in order to have the desanitation to take hold): have your mission execute a DOSCRIPT with a single line: local myTest = os.time() If your mission balks at this, you still haven't correctly de-sanitized your installation. Now, returning to de-sanitizing os just to get a randomized seed for random is IMHO not worth the risk. I think you can get similar results simply by re-seeding random based on the current play time of the mission, i.e. you wait with all your random stuff until something that depends on the actions of the players (like moving outside of a zone) to provide the seed number via timer.getTime() . Since players invariably don't take the same amount of time to do the same thing (e.g. starting up their aircraft, time until they take off), that can be a good way to seed your random. The problem with this approach is obvious, of course: you can't randomize things that you may want to randomize at mission start, where you do not have a random seed. I still think that depending on the availability of os simply to get a random seed is too risky. Edited February 19, 2023 by cfrag
AKA_Clutter Posted February 19, 2023 Author Posted February 19, 2023 (edited) Thanks for all the information. And I will consider alternate ways to generate a seed. I did try desanitizing os via commenting out the lines. Although the file looks like it took my changes, I still got the error. EDIT - I just looked at it (MissionScripting.lua) again, and it is back to the sanitized version. So not sure why it keeps changing MissionScripting.lua Again, thanks for the information and the recommendation. Edited February 19, 2023 by AKA_Clutter ---------------- AKA_Clutter Win 10 Pro, Intel i7 12700k @4.6 GHz, EVGA RTX 3080 FTW, Ultra 64 GB G.Skill DDR4 3600 RAM, Acer 27" flat screen, HP Reverb G2, TM Warthog HOTAS with Virpil warBRD base, MFG Rudder Pedals, Virpil TCS Rotor Base with AH-64Dcollective, TrackIR 5 Pro w/Vector Expansion, PointCTRL.
PravusJSB Posted February 23, 2023 Posted February 23, 2023 (edited) Here's an implementation for random seed, enjoy. (There are 3 bonus table library methods included too); -- UTILS -- random local MBIG = 2147483647 local MSEED = 161803398 local MZ = 0 local FAC = (1.0/MBIG) local inext = 0 local inextp = 31 local ma = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} local function math_randomseed(idum) local mj = MSEED - math.abs(idum) mj = mj % MBIG ma[55] = mj local mk = 1 for i = 1,54 do local ii = (21*i)%55 ma[ii] = mk mk = mj - mk if mk < MZ then mk = mk + MBIG end mj = ma[ii] end for k = 1,4 do for i = 1,55 do ma[i] = ma[i] - ma[1+(i+30)%55] if ma[i] < MZ then ma[i] = ma[i] + MBIG end end end inext = 0 inextp = 31 end local function math_random(...) local arg, tmp = {...}, 0 local mini = (not arg[2] and arg[1]) and arg[1] or (arg[1] and arg[2]) and arg[1] or 0 local maxi = (not arg[2] and arg[1]) and 1 or (arg[21] and arg[2]) and arg[2] or 0 if (mini > maxi) then tmp, maxi, mini = maxi, mini, tmp end inext = inext == 56 and 1 or inext+1 inextp = inextp == 56 and 1 or inextp+1 local mj = ma[inext] - ma[inextp] if mj < MZ then mj = mj + MBIG end ma[inext] = mj local f = mj * FAC; local range = 1+maxi-mini; return math.floor((f * range) + mini); end -- table.keys = function(t) local keys = {} local fmt = string.format for k in pairs (t or {}) do keys[#keys+1] = fmt("%s : (%s)\n",k,type(t[k])) end return table.concat(keys) end table.empty = function(t) local keys = 0 for k in pairs (t or {}) do if keys > 0 then break end keys = keys + 1 end return keys == 0 end table.count = function(t) local keys = 0 for k in pairs (t or {}) do keys = keys + 1 end return keys end -- I would prime the random function after seeding it and use as follows; math_randomseed() math_randomseed(os.time()) for j = 1,100 do math_random(3,20000) end for i = 1,2500 do math_random() end Edited February 23, 2023 by PravusJSB Creator & Developer of XSAF ::An AI model that wants to kill you, and needs no help from humans. Discord: PravusJSB#9484 twitch.tv/pravusjsb https://www.patreon.com/XSAF https://discord.gg/pC9EBe8vWU https://bmc.link/johnsbeaslu Work with me on Fiverr: https://www.fiverr.com/pravusjsb
Recommended Posts