Multiple Random Generations

Hello everyone. I have an issue. I have two functions that occasionally will be called at the same time. In the beginning of one function, I used os.time and for the other I use os.time*3.5 (just a random variant). However, when these functions are called (at the same time), the two random numbers are exactly the same.

Can anyone help with this?

Thanks in advance.

-Summit Tech

Why are you seeding the random number generator twice?  You only need to do it once in your main.lua when the app starts, unless you’re trying to intentionally get the same repeating set of numbers for some reason. 

Rob

I have two function. RandA and RandB. Now at the start of the game I get two random values, one from each one. However the two numbers are exactly the same. This happens whenever I call the functions at the same time. I need to be able to call both at the same time but end up with different numbers.

Since we don’t do treading, you can’t call them at the exact same time.  One has to happen before the other.   This should work for you:

math.randomseed( os.time() )

local random1 = RandA()

local random2 = RandB()

and get different values.  Perhaps if you posted some code, it might help us understand things a bit better.

Rob

Sure thing.

local mRand = math.random local function moveB1() math.randomseed( os.time() ) split2.degree1 = mRand(1,360) split2.xSpeed1 = math.cos(degree\*math.pi/180) split2.ySpeed1 = math.sin(degree\*math.pi/180) bubbleS1:setLinearVelocity(split2.xSpeed1\*impulseVal, split2.ySpeed1\*impulseVal) end local function moveB2() math.randomseed( os.time()\*42/13 ) split2.degree2 = mRand(1,360) split2.xSpeed2 = math.cos(degree\*math.pi/180) split2.ySpeed2 = math.sin(degree\*math.pi/180) bubbleS2:setLinearVelocity(split2.xSpeed2\*impulseVal, split2.ySpeed2\*impulseVal) end local function moreButtonCountSplit() if split2.timerLoop == 40 then -- give a force to both objects moveB1() moveB2() end end

That is the part I am currently working on.

However during the game whenever either object touches a wall it respectively receives a linear velocity. This means I simply cannot have both random numbers being generated in the same function. 

Just take the math.randomseed( os.time() ) out of each function and make it like the first line in your main.lua. 

Rob

I did that and the two generated numbers are always the same. I print out the two numbers and they are the same, even though moveB2 is called after moveB1.

Do you mean for:
 

split2.degree1 = mRand(1,360)

split2.xSpeed1 = math.cos( degree *math.pi/180)

to be:

split2.degree1 = mRand(1,360)

split2.xSpeed1 = math.cos( split2.degree1 *math.pi/180)

(and the same in the other function?)

Thanks Rob! A bit of carelessness on my part. Everything is running smoothly. Thanks again!

Quite welcome

I didn’t know that. Thanks Rob!

Why are you seeding the random number generator twice?  You only need to do it once in your main.lua when the app starts, unless you’re trying to intentionally get the same repeating set of numbers for some reason. 

Rob

I have two function. RandA and RandB. Now at the start of the game I get two random values, one from each one. However the two numbers are exactly the same. This happens whenever I call the functions at the same time. I need to be able to call both at the same time but end up with different numbers.

Since we don’t do treading, you can’t call them at the exact same time.  One has to happen before the other.   This should work for you:

math.randomseed( os.time() )

local random1 = RandA()

local random2 = RandB()

and get different values.  Perhaps if you posted some code, it might help us understand things a bit better.

Rob

Sure thing.

local mRand = math.random local function moveB1() math.randomseed( os.time() ) split2.degree1 = mRand(1,360) split2.xSpeed1 = math.cos(degree\*math.pi/180) split2.ySpeed1 = math.sin(degree\*math.pi/180) bubbleS1:setLinearVelocity(split2.xSpeed1\*impulseVal, split2.ySpeed1\*impulseVal) end local function moveB2() math.randomseed( os.time()\*42/13 ) split2.degree2 = mRand(1,360) split2.xSpeed2 = math.cos(degree\*math.pi/180) split2.ySpeed2 = math.sin(degree\*math.pi/180) bubbleS2:setLinearVelocity(split2.xSpeed2\*impulseVal, split2.ySpeed2\*impulseVal) end local function moreButtonCountSplit() if split2.timerLoop == 40 then -- give a force to both objects moveB1() moveB2() end end

That is the part I am currently working on.

However during the game whenever either object touches a wall it respectively receives a linear velocity. This means I simply cannot have both random numbers being generated in the same function. 

Just take the math.randomseed( os.time() ) out of each function and make it like the first line in your main.lua. 

Rob

I did that and the two generated numbers are always the same. I print out the two numbers and they are the same, even though moveB2 is called after moveB1.

Do you mean for:
 

split2.degree1 = mRand(1,360)

split2.xSpeed1 = math.cos( degree *math.pi/180)

to be:

split2.degree1 = mRand(1,360)

split2.xSpeed1 = math.cos( split2.degree1 *math.pi/180)

(and the same in the other function?)

Thanks Rob! A bit of carelessness on my part. Everything is running smoothly. Thanks again!

Quite welcome