Jump to content

yet another random generator function for you


Lineaxe
 Share

Recommended Posts

while I was digging the the lua books online I found this in a discussion and changed it over a bit so it returns a random number in the range of 1 thru the number you input into it. Heh, I liked the code and the concept they were talking about and so , here it is , in a function :pilotfly:

 

function randOSnumber(inpNumber)

return ( tonumber(tostring(os.time()):reverse():sub(1,6)) )% inpNumber +1

end --ENDOF

 

 

of course you can just take the code line out and use it by itself..

 

ok I played with it a bit and then came up with a larger function that let's you select which random generation style you want. There are 4 to choose from : os , math.random, a combo of both, and a 4th choice which is random selection of one of the first 3 choices ... that one potentially takes up the most cpu cycles of them all . so if you have a bit of time like at a start of mission ,

well , I pick 4 myself . that way I can debug the other 3 :) anyhow here is the code for it ... I am still testing , it might have a bug in it I can't see yet. Will test out a few other methods and add them in at some point.

 

function randOSnumber(inpNumber,inpType) -- valid integers 1 thru 4 for now

bflagit=false; inpType =math.abs(math.floor(inpType)) -- validation - absolute of integer portion only

if (inpType>4 or inpType<1) then BASE:T({"inpType is not a 1,2,3 or 4: "..tostring(inpNumber)}) return end

 

while (bflagit==false ) do

 

if (inpType==1) then return ( tonumber(tostring(os.time()):reverse():sub(1,6)) )% inpNumber +1

elseif (inpType==2) then return math.random(inpNumber)

elseif (inpType==3) then xx=( tonumber(tostring(os.time()):reverse():sub(1,6)) ) return math.random(xx)% inpNumber +1

elseif (inpType==4) then inpType=( tonumber(tostring(os.time()):reverse():sub(1,6)) )% 3 +1

end

end

bflagit=true -- never should get here , just returns in types 1,2 or 3

end --ENDOF randOSnumber


Edited by Lineaxe
clean up a bit
Link to comment
Share on other sites

just learned something new after the upgrade .

 

you have to comment out the line inside missionscripting.lua that

sanitizes the 'os' module and thus wont let the code run ..I can see why

they might want to do that. however I comment it out for the mission that

I am writing up , anyhow. Much more power at your fingertips. It's like adding in the NA into XNA !!

Link to comment
Share on other sites

well , that method is ONE of the ones that the random selector chooses from. just read the code and you can see that is so. This generator gives someone the OPTION of using a few throughout their code. not just one kind of generator.

Link to comment
Share on other sites

I have perceived that the DCS random number generator has a bias. In a random selection of 3 units the first unit in the list seemed to be selected out of proportion to true randomness. Not sure about math.random().

Link to comment
Share on other sites

I have perceived that the DCS random number generator has a bias. In a random selection of 3 units the first unit in the list seemed to be selected out of proportion to true randomness. Not sure about math.random().

 

You've right ! I noticed that when testing a function to get a random position with conditions of flat and altitude. When I do nothing in DCS, except getting positions, there's a biais. Grimes in a post said you should add lines math.random in a script to be sure it's really random. You can't reset the generator because os library is sanitized.

 

https://forums.eagle.ru/showpost.php?p=2638821&postcount=3

 

So I created a small timer to do a math.random at math.random repeated delay fixed between .2 et 1 second. That's better now. But the best way is to have a reset generator possibiliy equivalent to math.randomseed(os.time())

 

Sunski


Edited by sunski34
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...