Matrix Games Forums

Forums  Register  Login  Photo Gallery  Member List  Search  Calendars  FAQ 

My Profile  Inbox  Address Book  My Subscription  My Forums  Log Out

is randomseed still needed?

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
  Printable Version
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion >> is randomseed still needed? Page: [1]
Login
Message << Older Topic   Newer Topic >>
is randomseed still needed? - 1/22/2019 3:45:13 AM   
Whicker

 

Posts: 664
Joined: 6/20/2018
Status: offline
I was under the impression that you need to `seed` the random function like so:

math.randomseed(os.time())
for i = 1,10 do
local r = math.random(1,10)
print(r)
end
print("---")

if you didn't have that first line then it would pick a random number once - but on subsequent tries it would continue picking the same number - something like that.

in actually testing this though it seems to work better without the randomseed - like the game is already doing that?

has anything changed? is the game automatically doing randomseed now? seems like it should.

If I run that code above in the console but without the randomseed part I get better results. Tried with Tool_EmulateNoConsole(true) as well to make sure it wasn't just the console.
I've tried this multiple times on new blank scenarios to make sure the seeding was not there from my code. I also see in researching this that you should only seed once at the start and then not again - little vague on if that would be only on the initial scen load or each time a scen is loaded, assume the latter but it kinda looks like it is not needed anyways.

The most obvious part where randomseed seems most wrong is when you click run a bunch times in less than a second - the numbers are all the same. Clearly you are not supposed to re-seed.



Post #: 1
RE: is randomseed still needed? - 1/22/2019 6:37:45 AM   
KnightHawk75

 

Posts: 1361
Joined: 11/15/2018
Status: offline
In theory per some docs you set the seed once per application load\session and you're supposed to be good, in practice, idk.

Let's have a look.
I duped all your cases, same results as you. I ran tests with 'improved' versions mentioned below and got solid random results as well (all tests were click-fast couple times with in same second). Conclusion was same as you, seemed at a glance the game might be initializing it for us.

I went snooping around in the game code a little.
Looking at the LuaSandbox.Initialize code after it registers all the commands it does do this:
quote:

this.lua.DoString("math.randomseed( os.time() )", "chunk");


So I think the answer is yes, upon lua environment initialization it does initialize the randomseed using os.time() as the seed for us, and no we don't in 'most cases' need to touch it anymore.
When or if that was changed I don't know, it may have or may not have made the release notes at some point in time. Basically the generic LUA docs are correct, but Command is doing it for us so we don't have too, which I agree is good. That probably should be documented\updated somewhere if it's not already.

If you want\need more entropy the 'improved' functions may be the way to go.
I came across these and used them in the past improvedrandom() and improvedrandomseed() ?
Seems I can't post links yet so maybe I can try it this way
 github dot com /rjstone/cmano-lua-helpers/blob/master/cmano_helpers.lua

Those functions would go in start-up script.


< Message edited by KnightHawk75 -- 1/22/2019 6:44:27 AM >

(in reply to Whicker)
Post #: 2
RE: is randomseed still needed? - 1/22/2019 5:08:42 PM   
Whicker

 

Posts: 664
Joined: 6/20/2018
Status: offline
Interesting - thanks for testing it. I don't think I am doing any random numbers in a loop where this would be a problem, but I can think of places where you could.

here is that link for others, I was looking at that way of doing random numbers as well.

https://github.com/rjstone/cmano-lua-helpers

some good stuff there- in the helpers file. I like some of those functions.

(in reply to KnightHawk75)
Post #: 3
RE: is randomseed still needed? - 1/23/2019 1:35:43 AM   
Whicker

 

Posts: 664
Joined: 6/20/2018
Status: offline
hmmm. Not being the trusting type a made a new scen, added one side and then made an event that triggers every 5 seconds and has the following loop in it.

local r
for i = 1,10 do
r = math.random(1,100)
print(r)
end
print("---")


Results - not particularly random:
1
57
20
81
59
48
36
90
83
75
---
1
57
20
81
59
48
36
90
83
75
---
1
57
20
81
59
48
36
90
83
75
---
18
86
72
52
31
2
10
37
15
17
---
18
86
72
52
31
2
10
37
15
17
---
1
57
20
81
59
48
36
90
83
75
---

I then ran the same code in the lua console and got much better results. Then went back to the game thinking maybe that would have seeded the games event, no real change - though maybe it was slightly different.
I then made a second event to do the seed - once - and that didn't seem to make a difference.

Then I added the seed part to the top of my random generator code and now I am getting random numbers. Which is the way I have always done it, but in reading about it seems like it is incorrect.
You would think a random number would be trivial for a computer - same as dates/time, amazing how complicated things can be.

(in reply to Whicker)
Post #: 4
RE: is randomseed still needed? - 1/23/2019 9:50:20 PM   
KnightHawk75

 

Posts: 1361
Joined: 11/15/2018
Status: offline
Glad you were persistent.

Interesting the difference between console and non console you found without the manual call to re-seed in either. Almost like it gets added per 'run' somehow in the console?

You would think that inside the console then, if we somehow simulated a 5 second delay without leaving a singular execution then we would also see the same problem right?

New scenario: goodguys\badguys defined nothing else other than a title.
I used the attached globalstarup script on scene load.(repeatable\active). [it stores ostime and then makes single call to math.randomseed(storedtime)]
I used the attached 5secondevent action script on every5second event. [it does your base script with some formatting differences that make patterns easier to search in txt]
Saved. clear logs. reopen file in scenario editor via start menu.
Let run for 2 minutes. lots of dupes just like you got.

Paused game (doesn't seem to technically matter).
Went into console.. ran the extra script attached that has it's own sort of embedded timer\thread blocking of a while loop that only ends after 5 seconds. Output from that sample for me is random for that 'run' or additional of it, at least testing up to 24 iterations, or 120seconds. Whats up with that? I would think I'd see repeats since even if console gets a new seed per-run all the iterations are all in the same run\same seed? confusing

So best to just re-seed regardless of context I guess?


Attachment (1)

(in reply to Whicker)
Post #: 5
RE: is randomseed still needed? - 1/23/2019 11:21:49 PM   
Whicker

 

Posts: 664
Joined: 6/20/2018
Status: offline
I suppose. Just disturbing that the console works so well and is so different from the game.

It is confusing that is for sure.

I'm thinking the seed once really means don't seed inside a loop - seed outside the loop, but you need to seed in the function/code you are running at the top. This matches all examples I have seen about seeding for cmano, just reading about it on the web in general makes it seem like you would just do it once in the game on scen load.

(in reply to KnightHawk75)
Post #: 6
Page:   [1]
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion >> is randomseed still needed? Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts


Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI

0.297