random seed or shuffle bag for quiz show app?

Hey guys. Pretty new to programming so forgive me if this seems overly basic in its wording. Here goes…

I was wondering if anyone had any experience using a shuffle bag for random item selection. Can anyone tell me if you can use “shufflebag” to pull questions randomly from a table? My game is structured as follows:

main.lua > mainMenu.lua > menu.lua > game.lua > categories.lua > utils.lua > questions.lua

The mainMenu is my start screen. When start is pressed it goes to menu, which is the list of categories. My questions for each category are stored in individual tables in their own lua files. Ex: Bonus.lua. 

Currently I am using this to select random questions from the table. 

local function shuffle(t)

    local rand = math.random

    for i = #t, 2, -1 do

        local j = rand(i)

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

    end

end

table.shuffle = shuffle

I store this in it’s own utils.lua file. In each question category I am calling it with table.shuffle (table name)

The problem is it only randomly “shuffles” the questions once. After that it’s in the same order every time unless I quit the app completely. I saw the video on the “shufflebag” https://youtu.be/WTirbM9RRLE?t=5m16s and thought I could use something like this. Can anyone tell me how I could implement this with the way my game is currently structures. I can not figure out how to make this access the questions in my table lua files and display one randomly selected question at a time. 

Ultimately I would want there to be 5 randomly selected questions during each game  (one at a time) from a large bucket of say 50 questions. 

Here is an example of my question table


– QUESTIONS


– Cat questions for bonus table

local Bonus = {

{

question = “Test Question 1.”, – The actual question text

image = nil,

answer = 1, – answer for the question

answers = {

“This one is correct”,

“Generic Answer”,

“Generic Answer”,

“Generic Answer”,

},

},

{

question = “Test question 2.”,

image = nil,

answer = 2,

answers = {

“Generic Answer”,

“This one is correct”,

“Generic Answer”,

“Generic Answer”,

},

},

{

question = “Test question 3.”,

image = nil,

answer = 3,

answers = {

“Generic Answer”,

“Generic Answer”,

“This one is correct”,

“Generic Answer”

},

},

{

question = “Test question 4!”,

image = nil,

answer = 1,

answers = {

“Generic Answer”,

“Generic Answer”,

“Generic Answer”,

“Generic Answer”,

“Generic Answer”,

“Generic Answer”

},

},

{

question = “Test question 5.”,

image = nil,

answer = 1,

answers = {

“Generic Answer”,

“Generic Answer”,

“Generic Answer”,

},

},

{

question = “Test question 6?”,

image = nil,

answer = 2,

answers = {

“False”,

“True”,

},

},

}

table.shuffle(Bonus)

–Return it now

return Bonus

Any help or pointing me in the right direction would be greatly appreciated!

Hi @salfenito,

Do you want to simply “seed” the random number generator? If so, that’s easily done with the following command (and when you’re randomizing, you basically must call this to get true randomization going). :slight_smile:

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

Note that you only need to call this once, i.e. near the top of “main.lua”. It’s redundant to call it more than once.

Hope this helps,

Brent

If you’re pulling questions to present to the user and want to avoid duplicates, I’d:

  1. shuffle the set in advance and pull from it in order.
  • OR -
  1. Use a shuffle bag.

I tried this but is only re shuffles my questions if I quit the app. Going back to the category menu and re-selecting the same category presents the questions in the same order as the last time unless I close the app completely and start over. How do I fix that?

Can a shufflebag be use to shuffle a table? How do I reference each question? 

A reference to anything can be placed in a shuffle bag.

Well like I said, I am a total coding noob but I am starting to figure this stuff out.

I played around with shuffleBag and I mostly understand how it works. My problem is all the examples of shuffleBag involve a card game scenario and the code is all contained in the main lua. I can’t figure out how to get the shuffleBag to pull from a table of questions stored in a separate lua file. In my app, the next question is generated and displayed automatically when you select the answer for the current question. Maybe this I am just getting in over my head but any further help anyone could provide would be awesome.

I feel like I am close, just missing something in the concept here. 

Which examples are you talking about?  Can you link to them here?

Also, this article is not about shuffle bags but is completely applicable to your current problem:

https://coronalabs.com/blog/2014/09/30/tutorial-how-to-shuffle-table-items/

Also, if you’re using Composer (or really any Lua module would have the same issue), your shuffling code may not be re-executed.

In Lua, modules when they are first required has the code in the main chunk of the module executed. After that, when you require the module a second time, you only get a reference to the object that was returned from the first loading.  Thus if you have a module like:

local M = {} math.randomseed( os.time() ) local function shuffleTable( t ) &nbsp;&nbsp;&nbsp;&nbsp;local rand = math.random &nbsp;&nbsp;&nbsp;&nbsp;assert( t, "shuffleTable() expected a table, got nil" ) &nbsp;&nbsp;&nbsp;&nbsp;local iterations = #t &nbsp;&nbsp;&nbsp;&nbsp;local j &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;for i = iterations, 2, -1 do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j = rand(i) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t[i], t[j] = t[j], t[i] &nbsp;&nbsp;&nbsp;&nbsp;end end M.letters = { "A", "B", "C", "D" } shuffleTable(M.letters) --\<------ assuming you use the code from the shuffle tutorial above return M

No matter how many times you require that module after the first, shuffleTable() will have run just once until the module has been “unrequired” and there is no straight API for doing that.  A Composer scene is nothing more than a Lua module with some events and API calls to make scene transitions happen, so it has to obey all the rules for any other Lua scene.

If you have executable code in your Composer scene (not inside a scene:create() or scene:show() function which is called by events outside of the “require”), it gets run once. Because of other the way the scene objects are cached, scene:create() may also not get called every time a scene shows. Resetting scenes frustrates a lot of people trying to learn Composer. Getting your questions reshuffled sounds like it fits into the resetting scene category.

There is a simple way around it, its a brute force method, but it’s simple and it works until scene loading becomes painfully slow.  Simply remove the scene before you go to it.  From your menu scene, call:

composer.removeScene("yourgamescenename") composer.gotoScene("yourgamescenename", options ) --\<------- whatever options you normally would send, delay, transition, etc.

If you’re not going back to your menu afterwards, go to a cut-scene, i.e. level over, try again, next level, etc. and in that scene, remove the game scene just before you go to it.  

You cannot remove the scene you are currently in. Calling composer.removeScene() has to happen in some other scene.

Rob

Look at the complete examples in the bottom. Thanks for the link, I will look it over. 

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/shufflebag/

Very informative response! Thanks a ton. I have a lot to learn but this is great help!

Oh, I see.  You were referring to the example on my docs.

That is just one (common and easily understood) usage that many folks think of when first using shuffle bags.

Again, any reference can be inserted into a shuffle bag.  Your sub-tables with questions and answers would be a ‘reference’ in this case.

When I have time I’ll put together a sample showing how this would work for your scenario and post back.  So be sure to check this forum entry in a few days.

Oh man, that would be awesome! Thank you. 

If anyone here is interested in helping me learn solutions to some of these problems, I am willing to pay for some one on one lessons/help. I am not looking for someone to just do the work for me, I truly want to learn to understand how things like shuffle bag would work in my specific application. Contact me at shawn@spawndesignsolutions.com – Thanks. 

OK, the puzzle game sample is out.

https://www.youtube.com/watch?v=fW1zM3XVp9g

It does require SSK2 to run, but you can look at the code for free here:

https://github.com/roaminggamer/SSK2_QuizGames

Hi @salfenito,

Do you want to simply “seed” the random number generator? If so, that’s easily done with the following command (and when you’re randomizing, you basically must call this to get true randomization going). :slight_smile:

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

Note that you only need to call this once, i.e. near the top of “main.lua”. It’s redundant to call it more than once.

Hope this helps,

Brent

If you’re pulling questions to present to the user and want to avoid duplicates, I’d:

  1. shuffle the set in advance and pull from it in order.
  • OR -
  1. Use a shuffle bag.

I tried this but is only re shuffles my questions if I quit the app. Going back to the category menu and re-selecting the same category presents the questions in the same order as the last time unless I close the app completely and start over. How do I fix that?

Can a shufflebag be use to shuffle a table? How do I reference each question? 

A reference to anything can be placed in a shuffle bag.