How to randomize scene with out repeat?

Hello

I have this mini quiz game were there is 10 short questions. Ok what I need help is I want to the quiz to go through 10 scenes but without repeating So after going through 10 scenes it then switches to the results.

Example: Scene2, Scene5, Scene 10, Scene1, Scene3, Scene9, Scene8, Scene4, Scene6, Scene7 then Results.lua

Anyone know how this attempt is done? [import]uid: 17058 topic_id: 33069 reply_id: 333069[/import]

Hi.

I think somebody posted shuffling code before, so there may be something better than this. Otherwise, the following should be about right:

[lua]local indices = {}
local order = {}

– Initial indices, all in order
for i = 1, 10 do
indices[i] = i
end

for n = 10, 1, -1 do – Iterate backwards because we’re removing elements
local slot = math.random(n) – Choose random slot from 1 to n

order[#order + 1] = indices[slot]

indices[slot] = indices[n] – Backfill the used-up slot so we don’t have to move elements down
end

– Now use ‘order’ to look up your scenes…[/lua] [import]uid: 27791 topic_id: 33069 reply_id: 131339[/import]

What do I put here director:changeScene() when switching scenes? [import]uid: 17058 topic_id: 33069 reply_id: 131341[/import]

Unless you need to do very specific things for each question, I would recommend you package the questions together in one scene. If you need to make a change you’ll have to go through all 10 scenes to do it, and you are probably redoing a bunch of stuff.

I could be wrong though, really depends on what you’re doing in the app.
[import]uid: 181948 topic_id: 33069 reply_id: 131356[/import]

Hi.

I think somebody posted shuffling code before, so there may be something better than this. Otherwise, the following should be about right:

[lua]local indices = {}
local order = {}

– Initial indices, all in order
for i = 1, 10 do
indices[i] = i
end

for n = 10, 1, -1 do – Iterate backwards because we’re removing elements
local slot = math.random(n) – Choose random slot from 1 to n

order[#order + 1] = indices[slot]

indices[slot] = indices[n] – Backfill the used-up slot so we don’t have to move elements down
end

– Now use ‘order’ to look up your scenes…[/lua] [import]uid: 27791 topic_id: 33069 reply_id: 131339[/import]

What do I put here director:changeScene() when switching scenes? [import]uid: 17058 topic_id: 33069 reply_id: 131341[/import]

Yes, what you want to do is “shuffle” the scene names.

Create a table that has the various scene names in it:

local sceneNames = {"scene1", "scene2", "scene3", "scene4"}

Then call this function:

local function shuffle(t)     local iterations = #t     local j     for i = iterations, 2, -1 do         j = math.random(i)         t[i], t[j] = t[j], t[i]     end end shuffle(sceneNames)

after that the scenes are randomized, no repeats.

[import]uid: 19626 topic_id: 33069 reply_id: 131388[/import]

Unless you need to do very specific things for each question, I would recommend you package the questions together in one scene. If you need to make a change you’ll have to go through all 10 scenes to do it, and you are probably redoing a bunch of stuff.

I could be wrong though, really depends on what you’re doing in the app.
[import]uid: 181948 topic_id: 33069 reply_id: 131356[/import]

Yes, what you want to do is “shuffle” the scene names.

Create a table that has the various scene names in it:

local sceneNames = {"scene1", "scene2", "scene3", "scene4"}

Then call this function:

local function shuffle(t)     local iterations = #t     local j     for i = iterations, 2, -1 do         j = math.random(i)         t[i], t[j] = t[j], t[i]     end end shuffle(sceneNames)

after that the scenes are randomized, no repeats.

[import]uid: 19626 topic_id: 33069 reply_id: 131388[/import]

Hi Rob,  

i tried your function to randomize numbers from 1 to 3 without repetition but I was getting repeated numbers: The code is below 

[lua]

for i=1, 3 do 

    postable[i]=math.random(3)

end

            

local function shuffle(t)

        local iterations = #t

            local j

            for i = iterations,2, -1 do

        j = math.random(i)

                t[i], t[j] = t[j], t[i]

    end

           

        end

        shuffle(postable)

    

for i = 1 , (#postable) do

    print(postable[i] )

end

[/lua]

can you please advice ?

Hi @alzaabi980,

Usually it helps to “seed” the random number generator, like shown here:

http://docs.coronalabs.com/api/library/math/randomseed.html

Hope this helps,

Brent Sorrentino

Hi.

You probably don’t want

for i=1, 3 do postable[i]=math.random(3) end

since math.random() may return the same number twice (or thrice, etc.). Rather, just populate your table as

for i=1, 3 do postable[i]=i end

That way you know you have a well-formed table. Then just shuffle that.

I generally do my strict randoms like so.  Not sure if it’s the perfect way though…

[lua]
–< Seed random

math.randomseed( os.time() )

–< Localize math.random

local mRan = math.random

–< Create the base table

local baseTable = { “scene1”, “scene2”, “scene3”, “scene4”, “scene5” }

–< Init final mixed table

local mixedTable = {}

–< Do work

for i=#baseTable, 1, -1 do

  --< Get a random position

  local tmpRan = mRan( #baseTable )

  --< Get the table value at position

  local tblVal = baseTable[tmpRan]

  --< Insert value in mixedTable

  table.insert( mixedTable, tblVal )

  --< Remove value from base table, to stop duplicates

  table.remove( baseTable, tmpRan )

end

–< Print out mixedTable

for j=1, #mixedTable do

  print( mixedTable[j] )

end

[/lua]

And if you want to add the results…

[lua]table.insert( mixedTable, “results.lua” )[/lua]

But it would probably be better to just look for the end of your scene table and deal with it then.

Cheers.

Hi Rob,  

i tried your function to randomize numbers from 1 to 3 without repetition but I was getting repeated numbers: The code is below 

[lua]

for i=1, 3 do 

    postable[i]=math.random(3)

end

            

local function shuffle(t)

        local iterations = #t

            local j

            for i = iterations,2, -1 do

        j = math.random(i)

                t[i], t[j] = t[j], t[i]

    end

           

        end

        shuffle(postable)

    

for i = 1 , (#postable) do

    print(postable[i] )

end

[/lua]

can you please advice ?

Hi @alzaabi980,

Usually it helps to “seed” the random number generator, like shown here:

http://docs.coronalabs.com/api/library/math/randomseed.html

Hope this helps,

Brent Sorrentino

Hi.

You probably don’t want

for i=1, 3 do postable[i]=math.random(3) end

since math.random() may return the same number twice (or thrice, etc.). Rather, just populate your table as

for i=1, 3 do postable[i]=i end

That way you know you have a well-formed table. Then just shuffle that.

I generally do my strict randoms like so.  Not sure if it’s the perfect way though…

[lua]
–< Seed random

math.randomseed( os.time() )

–< Localize math.random

local mRan = math.random

–< Create the base table

local baseTable = { “scene1”, “scene2”, “scene3”, “scene4”, “scene5” }

–< Init final mixed table

local mixedTable = {}

–< Do work

for i=#baseTable, 1, -1 do

  --< Get a random position

  local tmpRan = mRan( #baseTable )

  --< Get the table value at position

  local tblVal = baseTable[tmpRan]

  --< Insert value in mixedTable

  table.insert( mixedTable, tblVal )

  --< Remove value from base table, to stop duplicates

  table.remove( baseTable, tmpRan )

end

–< Print out mixedTable

for j=1, #mixedTable do

  print( mixedTable[j] )

end

[/lua]

And if you want to add the results…

[lua]table.insert( mixedTable, “results.lua” )[/lua]

But it would probably be better to just look for the end of your scene table and deal with it then.

Cheers.

Thanks Brent and StarCrunch ,

I did what you both recommended and finally I get the random function works without repeated values.

I appreciate your help

Abdul
 

Thanks Brent and StarCrunch ,

I did what you both recommended and finally I get the random function works without repeated values.

I appreciate your help

Abdul