Random value from table, don't repeat cell value, until all shown once

I would like to select a random cell value from a table and not repeat that cell value unless each value has already been shown once. This is what I have so far:

constrain attr
self.image to tableCellValue( game.Table 1 , game.my_style ,1)

rule when touch pressed
change att
game.my_style to random(1,tableRowCount( game.Table 1 ))


Thanks!

Comments

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880
    You have a at least different options.

    1) Shuffle the table rows until they are sufficiently random. Then retrieve the contents of the first row and use it as needed. Then delete the first row. (The second row now becomes the first row.) Then repeat the process.

    2) Shuffle the table rows. Then have a counter that takes the first row. Increment the counter by one. (That is counter = counter +1) And take the next row. Then repeat the process. Just go down row by row until it reaches the last row.

    3)Randomly find which row to use (as in your touch rule above). Get the contents of the row. Then delete that row. (And repeat)
  • SlowboySlowboy Member, PRO Posts: 329
    Sounds good @RThurman , but how do you shuffle table rows?
    Thanks!
    Wayne
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited January 2014
    I have a demo that shuffles table rows. If you can't find it in the Spare Code thread, notify me or send me a PM and I'll post it.

    Option #3 is MUCH easier than the other options, though.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880
    edited January 2014
    Here is a quick way. Basically it swaps a random row with row 1 (a bunch of times).

    Edit: @Tatiang beat me to it! His way is much more efficient. Go with that one.
Sign In or Register to comment.