Can someone explain math.randomseed

I am using the math.random function in my game to implement a random decision.

  
local numHowManyBalls = allBallsGroup.numChildren  
  
local numRandomIndex = math.random(1, numHowManyBalls)  
  

I see there is another function called math.randomseed and I would like to know what this is for and hence if I should be using it and how?

I have other functions in the game that also call the random function.

Thanks

Paul [import]uid: 7863 topic_id: 7217 reply_id: 307217[/import]

math.randomseed(N) sets N as the seed for the pseudo random number generator.
behind the scenes all “random” number generators use a seed

math.randomseed(5) will always return the same sequence of numbers, while

math.randomseed(os.time()) different sequences, since os.time() is a unique number
[import]uid: 6459 topic_id: 7217 reply_id: 25394[/import]

Thanks tetu

I am guessing I definitely need to use randomseed

Would I be fine just adding the following near the beginning of my main.lua file and this would take care of all the different scenarios where I generate random numbers in my game?

[code]

math.randomseed(os.time())
[/code] [import]uid: 7863 topic_id: 7217 reply_id: 25399[/import]

yes, put the random seed call near the beginning of main.lua
:slight_smile:

[import]uid: 6459 topic_id: 7217 reply_id: 25412[/import]