Help: Random number that doesn't repeat

Hello guys, 

I want to know if how can I do a random number using math.random() or other ways, that if it sees that the number has been already returned from the previous number randomization, it will do math.random again.

Let say I do math.random(1, 10) then on the first loop it returns 2,on second loop it returns 1, on the third loop it returns 2 again, since 2 is already given it will just loop again for another number.

Because I’m planning to have a game that will give the player a random set of questions, then every time the player answers that particular question correctly, I will marked that question and will not be ask again from the player.

I do get the logic, but I can’t express it in code :frowning:

What is the best way to do this? I’ve been doing it for like 3 hours but still no luck.

Your help will be much appreciated

Thanks in advance,

Jam

Check the following thread : http://forums.coronalabs.com/topic/37122-non-duplicate-randomiser/?hl=random

Almost same question was discussed and I recall there was a great solution proposed. Hope it works for you as well. 

What you want to do is called “Shuffling”.  Its like a deck of cards.  You have a fixed number and can only use one once.  If you search for “shuffle table” you will find many different ways of shuffling your data.  Now you do have to set up a table with your numbers like:

local myNumbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

Then you shuffle the table and it gets randomized.  Then you just iterate over the table in your for loop getting the numbers.

firstly thanks for the reply,

I searched for the shuffle table and i found your post

http://forums.coronalabs.com/topic/27576-how-to-randomize-scene-with-out-repeat/?hl=%2Bshuffle+%2Btable#entry148337

this does work,  thanks for that :slight_smile:

but this time I want to see in the terminal the shuffled items.

and If you don’t bother would explain some of the codes in you syntax:

local sceneNames = {“scene1”, “scene2”, “scene3”, “scene4”}

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]  --<< this one? what does it do?
end

end

shuffle(sceneNames)

thank you very much for your patience and time.

Jam

j = math.random(i)&nbsp; -- Notice I changed this..... t[i], t[j] = t[j], t[i] &nbsp;-- \<\< this one? what does it do? &nbsp;

Lua allows you to set multiple values at once.  In this case, it’s basically swapping the two values:

t – is the table reference passed to the function.

i and j are indexes into the table.

In this case, it’s simply taking t[j] and putting it into t[i], while at the same time taking t[i] and putting it in t[j].   This seems weird because you would think it would overwrite each other.  In traditional languages you would do:

local tmp = t[i]

t[i] = t[j]

t[j] = tmp

But this Lua short cut bypasses it.

I also noticed that the code is just calling a function called “random”, which I’m guessing in the code I copied that from, there was a line above that’s not in the sample that reads:

local random = math.random

which localizes random so it’s not a table lookup to find it.  I’ve updated the code to simply call math.random instead.

thanks Rob for the explanations.

You really are a big help :slight_smile:

Check the following thread : http://forums.coronalabs.com/topic/37122-non-duplicate-randomiser/?hl=random

Almost same question was discussed and I recall there was a great solution proposed. Hope it works for you as well. 

What you want to do is called “Shuffling”.  Its like a deck of cards.  You have a fixed number and can only use one once.  If you search for “shuffle table” you will find many different ways of shuffling your data.  Now you do have to set up a table with your numbers like:

local myNumbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

Then you shuffle the table and it gets randomized.  Then you just iterate over the table in your for loop getting the numbers.

firstly thanks for the reply,

I searched for the shuffle table and i found your post

http://forums.coronalabs.com/topic/27576-how-to-randomize-scene-with-out-repeat/?hl=%2Bshuffle+%2Btable#entry148337

this does work,  thanks for that :slight_smile:

but this time I want to see in the terminal the shuffled items.

and If you don’t bother would explain some of the codes in you syntax:

local sceneNames = {“scene1”, “scene2”, “scene3”, “scene4”}

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]  --<< this one? what does it do?
end

end

shuffle(sceneNames)

thank you very much for your patience and time.

Jam

j = math.random(i)&nbsp; -- Notice I changed this..... t[i], t[j] = t[j], t[i] &nbsp;-- \<\< this one? what does it do? &nbsp;

Lua allows you to set multiple values at once.  In this case, it’s basically swapping the two values:

t – is the table reference passed to the function.

i and j are indexes into the table.

In this case, it’s simply taking t[j] and putting it into t[i], while at the same time taking t[i] and putting it in t[j].   This seems weird because you would think it would overwrite each other.  In traditional languages you would do:

local tmp = t[i]

t[i] = t[j]

t[j] = tmp

But this Lua short cut bypasses it.

I also noticed that the code is just calling a function called “random”, which I’m guessing in the code I copied that from, there was a line above that’s not in the sample that reads:

local random = math.random

which localizes random so it’s not a table lookup to find it.  I’ve updated the code to simply call math.random instead.

thanks Rob for the explanations.

You really are a big help :slight_smile:

Here is also a very interesting LUA example how to generate "N different random numbers from the set 1…M"

http://lua-users.org/wiki/RandomSample

To be honest i do not really understand the code but it works great :slight_smile:

Here is also a very interesting LUA example how to generate "N different random numbers from the set 1…M"

http://lua-users.org/wiki/RandomSample

To be honest i do not really understand the code but it works great :slight_smile: