Jump to content

Recommended Posts

Posted (edited)
30 minutes ago, trevoC said:

You don't need an "unpredictable seed value" to seed the random number generator. "1234" works fine. Anything actually works.

I believe that you may misunderstand how random seeding works.

Quote

A pseudorandom number generator's number sequence is completely determined by the seed: thus, if a pseudorandom number generator is reinitialized with the same seed, it will produce the same sequence of numbers.

That means that if you use the same seed, the random number sequence is the same every time. If your random number generator isn't seeded with different values, the random() invocations always produce the same random number sequence each time that you run the mission. If, for example, the seed "1234" produces the random sequence 1, 7, 3, 5, 3 the first time you run the mission, it will do so again the next time and any other time that you use that seed. That is why it is so important to have random seeded with a different number each time you run a mission. The good thing about seeding is that if you use the same seed, you get the same sequence, which is great for testing. But bad for replay-ability because all your "randomized" units will always end up in the same place.

The good news here is that DCS missions seem to start with random seeded with a unique value each time, so apparently there is no need to invoke randomseed() in your missions any more.

Edited by cfrag
  • Thanks 1
Posted (edited)
On 2/22/2023 at 7:41 PM, trevoC said:

I'm not familiar with the mission creator or what portion of the API is available, but I'm sure there is some constantly changing integer value you can feed randomseed()

Well, it would of course help if you knew Mission Editor and it's limitations before you are kindly offering up advice or provide helpful comments about leading horses to water. This entire thread is about the fact that there is no "constantly changing integer value you can feed randomseed()". That was the entire point of OP's question.

Anyway, it's good that this is resolved.

Edited by cfrag
  • Like 1
Posted

All of this I discussed above. I'm not going to get into a <profanity>ting match with you. I've not only suggested every item you've mentioned but also offered to look at the OP's code and fix it if necessary.

And there is a constantly changing integer value you can feed randomseed as suggested above. Thanks for googling randomseed to repeat what I've explained above. All I've learned here is that helping people on this forum is a lose lose situation.

AMD 7900x3D | Asus ROG Crosshair X670E Hero | 64GB DC DDR5 6400 Ram | MSI Suprim RTX 4090 Liquid X | 2 x Kingston Fury 4TB Gen4 NVME | Corsair HX1500i PSU | NZXT H7 Flow | Liquid Cooled CPU & GPU | HP Reverb G2 | LG 48" 4K OLED | Winwing HOTAS

  • 1 year later...
Posted (edited)

I once found myself in a similar pickle, trying to make my scripts less predictable. After a bit of trial and error, I stumbled upon a neat trick – using a heads or tails coin method. Just like flipping a coin, it adds that element of randomness without relying on the same old methods. Sure, it might seem a bit unconventional at first, but sometimes thinking outside the box is the key. So next time you're stuck in a loop with math.random(), why not give it a shot? Who knows, you might just strike scripting gold!

Edited by TommyWillson
Posted
4 hours ago, TommyWillson said:

After a bit of trial and error, I stumbled upon a neat trick – using a coin method. Just like flipping a coin, it adds that element of randomness without relying on the same old methods.

That sounds intriguing. Can you elaborate how that works so that, for example two consecutive runs of the same mission create different results? I'm always looking for good alternatives like this.

  • 2 months later...
Posted (edited)

So I was not super satisfied with previous suggestions, including my own, and set out to try to do a decent way to seed  math.randomseed() in the DCS Scripting Environment.

The TLDR that surprised me a bit is:  "Don't worry, be happy.  math.randomseed() appears to be broken in the DCS scripting engine because it has been replaced by something custom behind the scenes.   If LUA scares you use the trigger system ACTION  FLAG SET RANDOM VALUE (min, max) to generate a random number between min and max (inclusive), or if you are comfortable with LUA just use math.random() because it is pre-seeded, and math.randomseed() is broken so you can't use a custom seed anyway.

Trying to get a good seed for math.randomseed() appears to be totally irrelevant in the DCS Simulation Scripting Environment, because as far as I can tell math.randomseed() is broken there.   If you pass it either an integer variable, or even just an integer number, it throws an error saying that  randomseed  is a field with a nil value.   I did some test code, and what works in standard LUA doesn't work in DCS, and what works in DCS doesn't work in standard LUA.   It appears that math.randomseed has been replaced by something custom in DCS, so what you need to do is either use a Flag Set Random Value to get a random number, or just use math.random without math.randomseed in LUA code for DCS.

This

seedNumber = 1426
math.randomseed(seedNumber)
print(math.random())
seedNumber = 2753
math.randomseed(seedNumber)
print(math.random())
seedNumber = 2753
math.randomseed(seedNumber)
print("Seed Number 2753 results:")
print(math.random(1,10))
print(math.random(1,10))
print(math.random(1,10))
seedNumber = 1426
print("Seed Number 1426 results:")
math.randomseed(seedNumber)
print(math.random(1,10))
print(math.random(1,10))
print(math.random(1,10))
print(math.random(1,10))
print(math.random(1,10))
print(math.random(1,10))
print(math.random(1,10))
print(math.random(1,10))
print(math.random(1,10))
print(math.random(1,10))
print(math.random(1,10))

Produces this in LUA

0.11670539583512
0.50927697496681
Seed Number 2753 results:
7
4
5
Seed Number 1426 results:
5
6
5
1
3
3
3
5
7
7
9

math.randomseed(integerVariable) works as expected.

In DCS this

-- Random seeder,  requires on mission start triggers that 
   --1: Use an action set random flag to set a flag called seedPickFLat to a default of zero  **deprecated you can just use step 2
   --2: Use a Flag Set Random Value action to set seedPickFlag to a range from 1 to 16

randomLookUp16 = {1426, 2753, 6999, 9779, 5702, 6384, 9497, 2072, 7419, 1545, 1302, 6354, 4985, 1997, 4395, 3717}  --table of 16 theoretically truly random numbers
 
seedPick = (trigger.misc.getUserFlag("seedPickFlag"))  --gets a random number from the flag and assigns it to a variable to pick a key from the lookup table

trigger.action.outText( "seedPick is " .. seedPick, 20, false)   --test message, comment out after validation

seedNumber = randomLookUp16[seedPick]  --sets seedNumber equal to the value for the selected key in the lookup table

trigger.action.outText("seed Number is: " .. seedNumber, 20, false)  --test message, shows contents of seedNumber variable, comment out after validation

math.randomseed(seedNumber)  -- intended to seed random number generator,  Actual effect is to throw error: randomseed is a nil field.

Works in that using a Flag Set Random Value (1, 16) will generate a random key, and select a number from the lookup table of seed numbers, but when you try to use that number assigned to a variable  math.randomseed(integerVariable) throws an error saying randomseed is a nil field.  Literally cutting and pasting working code from a LUA compiler gives that result.   Valid input causes an error. 

Cut and paste that input from DCS to LUA and then math.randomseed() works as expected.  

 

My conclusion therefore is that if math.randomseed() from LUA still exists in DCS it is now behind the wall and cannot be accessed by normal users.  It doesn't matter though, because they appear to have set things up so that math.randomseed( os.time() ) or something equivalent gets run automatically before math.random(),  so all your random numbers in DCS scripts are correctly seeded, whether you want them to be or not.

 

So we get convenience and security in one tidy little package.

 

I'm not sure if I should feel happy or insulted that they idiot-proofed it so much that even I can't mess it up.   😉

 

 

Edited by esb77

Callsign "Auger". It could mean to predict the future or a tool for boring large holes.

 

I combine the two by predictably boring large holes in the ground with my plane.

  • Recently Browsing   0 members

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