Data Realms Fan Forums
http://forums.datarealms.com/

math.random not so random?
http://forums.datarealms.com/viewtopic.php?f=73&t=46308
Page 1 of 1

Author:  Pantera1993 [ Mon Dec 09, 2019 9:42 pm ]
Post subject:  math.random not so random?

I'm working on a script that chooses a random number between 1 to 4 and then does something based on that value. The issue is that I get the same values in the same order very time. For example, the game will give values 4, 2, 2, 3 in that order etc... Not quite sure what I can do to solve this issue.

Here's some code to look at. I've changed some variable names and other things so as to not give away any details at this point of what I'm working on.

Code:
function Activity:ValueSelect()

   self.Value = math.random(1,4)
   
   if self.Value == 1 then
      self.NextValue = stuff
      self.CurrentValue = self.Variable1
   elseif self.Value == 2 then
      self.NextValue = stuff
      self.CurrentValue = self.Variable2
   elseif self.Value == 3 then
      self.NextValue = stuff
      self.CurrentValue = self.Variable3
   elseif self.Value == 4 then
      self.NextValue = stuff
      self.CurrentValue = self.Variable4
   end   
end   


EDIT: Okay now it works. Solution is to use math.randomseed()) to generate a random seed for the function to use when generating numbers. My solution is to check a timer then pop off a random number before generating a number for the function. This works reliably.

Code:
function Activity:ValueSelect()

   math.randomseed(self.MissionTimer:LeftTillSimMS(8))                 --we generate the seed here
   math.random();                                            --pop off a number to ensure randomness
   self.Value = math.random(1,4)                                          --generate numbers for our function.
   
   if self.Value == 1 then
      self.NextValue = stuff
      self.CurrentValue = self.Variable1
   elseif self.Value == 2 then
      self.NextValue = stuff
      self.CurrentValue = self.Variable2
   elseif self.Value == 3 then
      self.NextValue = stuff
      self.CurrentValue = self.Variable3
   elseif self.Value == 4 then
      self.NextValue = stuff
      self.CurrentValue = self.Variable4
   end   
end   

Author:  Bad Boy [ Wed Dec 11, 2019 4:12 am ]
Post subject:  Re: math.random not so random?

Yeah, using randomseed is indeed the way to solve this, the usual thing to do is math.randomseed(os.time()); if you have access to it in CC.
That said, I'm kinda surprised you have to set the random seed, I'd thought cc dealt with that for you. I guess the complication of a game environment means you usually won't notice it cause some other random thing would have happened before.

Author:  Pantera1993 [ Wed Dec 11, 2019 5:17 pm ]
Post subject:  Re: math.random not so random?

I did try using os.time but from what I can tell the game doesn't have access to it, but the current solution works fine. Maybe with open source, someone who know what they're doing can look into checking how the game handles os.time.

Author:  Bad Boy [ Wed Dec 11, 2019 6:01 pm ]
Post subject:  Re: math.random not so random?

The game probably doesn't give you access to the os library for safety reasons, same reason the io library was removed. The real solution would be setting a random seed for lua when the game starts, which I'm surprised isn't a thing.
Which cc version were you using and if it's not B33 (which you can grab in the discord's official-dev-log channel), would you mind testing it on that for me?

Author:  Pantera1993 [ Wed Dec 11, 2019 8:04 pm ]
Post subject:  Re: math.random not so random?

I just started testing today on build 33, will let you know soon how it works out.

EDIT: Looks like you're right. Build 33 seems to generate a random seed itself.

Author:  Bad Boy [ Sat Dec 14, 2019 2:21 am ]
Post subject:  Re: math.random not so random?

Yeah, I'd guess weegee had to specify a random seed to get lua working properly with multiplayer. Thanks for looking into that for me :)

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/