View unanswered posts | View active topics It is currently Fri Jan 24, 2020 7:56 am



Reply to topic  [ 6 posts ] 
 math.random not so random? 
Author Message
User avatar

Joined: Wed Jul 06, 2011 5:11 pm
Posts: 226
Reply with quote
Post 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   


Mon Dec 09, 2019 9:42 pm
Profile

Joined: Fri Sep 10, 2010 1:48 am
Posts: 666
Location: Halifax, Canada
Reply with quote
Post 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.


Wed Dec 11, 2019 4:12 am
Profile
User avatar

Joined: Wed Jul 06, 2011 5:11 pm
Posts: 226
Reply with quote
Post 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.


Wed Dec 11, 2019 5:17 pm
Profile

Joined: Fri Sep 10, 2010 1:48 am
Posts: 666
Location: Halifax, Canada
Reply with quote
Post 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?


Wed Dec 11, 2019 6:01 pm
Profile
User avatar

Joined: Wed Jul 06, 2011 5:11 pm
Posts: 226
Reply with quote
Post 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.


Wed Dec 11, 2019 8:04 pm
Profile

Joined: Fri Sep 10, 2010 1:48 am
Posts: 666
Location: Halifax, Canada
Reply with quote
Post 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 :)


Sat Dec 14, 2019 2:21 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.020s | 13 Queries | GZIP : Off ]