[SOLVED] Help having random scene on Devices?

Hi

I have been facing problem with doing random scenes on devices. It does not randomize instead it goes by certain like this it goes

scene1
scene8
scene5

It does all the time when I test on my device. But when I do it on the simulator it works like a charm. Both scene1 and scene8 have the same method of changing scenes.

This is how I have my code setup

main.lua

display.setStatusBar (display.HiddenStatusBar)  
  
local director = require ("director")  
  
local mainGroup = display.newGroup()  
  
local function main()  
local mySceneTable = {"scene1", "scene2", "scene3", "scene4", "scene5", "scene6", "scene7", "scene8", "scene9", "scene10","scene11", "scene12", "scene13", "scene14", "scene15", "scene16", "scene17", "scene18", "scene19", "scene20","scene21", "scene22", "scene23", "scene24", "scene25", "scene26", "scene27", "scene28", "scene29", "scene30","scene31", "scene32", "scene33", "scene34", "scene35", "scene36", "scene37", "scene38", "scene39", "scene40","scene41", "scene42", "scene43", "scene44", "scene45", "scene46", "scene47", "scene48", "scene49", "scene50","scene51", "scene52", "scene53", "scene54"}  
 mainGroup:insert(director.directorView)  
 director:changeScene(director:changeScene(mySceneTable[math.random(1, table.getn(mySceneTable))]), "fade")  
 return true  
end  
  
main()  

When I use my device it always starts at scene1 then scene8 then scene5 etc… All the time

Here is an example how I have it in scene1

local mySceneTable = {"scene1", "scene2", "scene3", "scene4", "scene5", "scene6", "scene7", "scene8", "scene9", "scene10","scene11", "scene12", "scene13", "scene14", "scene15", "scene16", "scene17", "scene18", "scene19", "scene20","scene21", "scene22", "scene23", "scene24", "scene25", "scene26", "scene27", "scene28", "scene29", "scene30","scene31", "scene32", "scene33", "scene34", "scene35", "scene36", "scene37", "scene38", "scene39", "scene40","scene41", "scene42", "scene43", "scene44", "scene45", "scene46", "scene47", "scene48", "scene49", "scene50","scene51", "scene52", "scene53", "scene54"}  
  
two:addEventListener("collision", two)  
function two:collision(event)  
if event.phase == "began" and event.other.hit == "answer" then  
display.remove(two)  
local function appear()  
local two2 = display.newImageRect("Numbers/2.png", 75, 100)  
two2.x = 360  
two2.y = 140  
--transition.to(two2,{time=500, xScale=0.75, yScale=0.75})  
localGroup:insert(two2)  
local function nextscene()  
director:changeScene(mySceneTable[math.random(1, table.getn(mySceneTable))])  
end  
timer.performWithDelay(2000, nextscene, 1)  
end  
timer.performWithDelay(10, appear, 1)  
end  
end  

That how I change my scene above I dont understand why it does not randomize

Also here is scene8

local mySceneTable = {"scene1", "scene2", "scene3", "scene4", "scene5", "scene6", "scene7", "scene8", "scene9", "scene10","scene11", "scene12", "scene13", "scene14", "scene15", "scene16", "scene17", "scene18", "scene19", "scene20","scene21", "scene22", "scene23", "scene24", "scene25", "scene26", "scene27", "scene28", "scene29", "scene30","scene31", "scene32", "scene33", "scene34", "scene35", "scene36", "scene37", "scene38", "scene39", "scene40","scene41", "scene42", "scene43", "scene44", "scene45", "scene46", "scene47", "scene48", "scene49", "scene50","scene51", "scene52", "scene53", "scene54"}  
  
two:addEventListener("collision", two)  
function two:collision(event)  
if event.phase == "began" and event.other.hit == "answer" then  
display.remove(two)  
local function appear()  
local two2 = display.newImageRect("Numbers/5.png", 75, 100)  
two2.x = 360  
two2.y = 140  
--transition.to(two2,{time=500, xScale=0.75, yScale=0.75})  
localGroup:insert(two2)  
local function nextscene()  
director:changeScene(mySceneTable[math.random(1, table.getn(mySceneTable))])  
end  
timer.performWithDelay(2000, nextscene, 1)  
end  
timer.performWithDelay(10, appear, 1)  
end  

Both scene1 and 8 are similar except with image change but I put just in case someone finds the mistake [import]uid: 17058 topic_id: 30544 reply_id: 330544[/import]

Hey sebitttas,

I believe all you need to fix your woes is this line at the top of your code;

[lua]math.randomseed( os.time() )[/lua]

This will regenerate a new randomseed each time it is called on your OS device.

I’m not sure on exactly how it works but I know it solved my randomization issues on device.

Brian. [import]uid: 40731 topic_id: 30544 reply_id: 122351[/import]

Thanks for the help now it works like a charm :slight_smile: [import]uid: 17058 topic_id: 30544 reply_id: 122357[/import]

Hey sebitttas,

I believe all you need to fix your woes is this line at the top of your code;

[lua]math.randomseed( os.time() )[/lua]

This will regenerate a new randomseed each time it is called on your OS device.

I’m not sure on exactly how it works but I know it solved my randomization issues on device.

Brian. [import]uid: 40731 topic_id: 30544 reply_id: 122351[/import]

Thanks for the help now it works like a charm :slight_smile: [import]uid: 17058 topic_id: 30544 reply_id: 122357[/import]