random number without repetition

Hey guys,

I hv searched the forum for a whole afternoon,

I still cannot find any topic solve my question

If i hv a table 

table={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}

how can i randomly choose of the the value from the table without repetition?

I just wanna get 1 to 15 randomly and without repetition…

plz give me some hints

So you want each number to be drawn once only? I won’t give you hints, I’ll go one better and tell you how to do it.

Each time you select a number, just remove it from the table.

myTable={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} for i = 1, 15 do --#myTable will be the number of remaining entries in the table local index = math.random(1, #myTable) --now use the index generated above to select one of your numbers local myRandomNumber = myTable[index] print(myRandomNumber) --and then remove than entry from the table table.remove(myTable, index) end

Also, I wouldn’t advise naming a table “table”, as it is a Lua keyword and you may cause yourself problems later on.

When I try you code, i find that all printed value is nil

However, i try to do it myself and find the answer finally

t={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}

for i=1 ,15 do

    

j = math.random(i)  

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

end 

I had a typo, which I’ve now edited. It should have said:

local myRandomNumber = myTable[index]

and not

local myRandomNumber = myTable.index

I confused about the solution you posted though. It doesn’t seem to be solving the problem you posted.

Instead it’s swapping elements in the table, and at the start of the loop it’s likely that it will just be swapping one entry with the same entry.

For example, in your loop when i == 1

j = math.random(i)  will also be 1, because the random values are between 1 and 1. 

Then you swap entry 1 with entry 1, which is obviously not going to do anything.

t[i], t[j] = t[j], t[i] --AKA t[1], t[1] = t[1], t[1]

Even when i == 2, you have a 50% chance of nothing happening, as the only possibilities are:

t[2], t[1] = t[1], t[2]  --swap entries t[2], t[2] = t[2], t[2]  --nothing happens

and in all cases you have not overcome your problem with repetition.

Now that I’ve edited my post (and actually tested it out), I know that my method definitely works.

So you want each number to be drawn once only? I won’t give you hints, I’ll go one better and tell you how to do it.

Each time you select a number, just remove it from the table.

myTable={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} for i = 1, 15 do --#myTable will be the number of remaining entries in the table local index = math.random(1, #myTable) --now use the index generated above to select one of your numbers local myRandomNumber = myTable[index] print(myRandomNumber) --and then remove than entry from the table table.remove(myTable, index) end

Also, I wouldn’t advise naming a table “table”, as it is a Lua keyword and you may cause yourself problems later on.

When I try you code, i find that all printed value is nil

However, i try to do it myself and find the answer finally

t={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}

for i=1 ,15 do

    

j = math.random(i)  

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

end 

I had a typo, which I’ve now edited. It should have said:

local myRandomNumber = myTable[index]

and not

local myRandomNumber = myTable.index

I confused about the solution you posted though. It doesn’t seem to be solving the problem you posted.

Instead it’s swapping elements in the table, and at the start of the loop it’s likely that it will just be swapping one entry with the same entry.

For example, in your loop when i == 1

j = math.random(i)  will also be 1, because the random values are between 1 and 1. 

Then you swap entry 1 with entry 1, which is obviously not going to do anything.

t[i], t[j] = t[j], t[i] --AKA t[1], t[1] = t[1], t[1]

Even when i == 2, you have a 50% chance of nothing happening, as the only possibilities are:

t[2], t[1] = t[1], t[2]  --swap entries t[2], t[2] = t[2], t[2]  --nothing happens

and in all cases you have not overcome your problem with repetition.

Now that I’ve edited my post (and actually tested it out), I know that my method definitely works.