|
| 13 Aug 2013 08:28 PM |
Use a table, select each one in a random order, without possibly choosing the same item twice
Table={1,2,3,4,5,6}
for i,v in pairs(Table) do e = math.random(1,#Table) print(e) end
That has the technical possibility of printing:
2 2 2 2 2 2
It reoccurred 6 times but did not refrain to use different numbers each time. I want this to randomly select out of the table without picking something more than once. Help? |
|
|
| Report Abuse |
|
|
magnalite
|
  |
| Joined: 18 Oct 2009 |
| Total Posts: 2467 |
|
| |
|
|
| 13 Aug 2013 08:36 PM |
Table={1,2,3,4,5,6}
for i,v in pairs(Table) do e = math.random(1,#Table) print(e) table.remove(Table, e) end |
|
|
| Report Abuse |
|
|
magnalite
|
  |
| Joined: 18 Oct 2009 |
| Total Posts: 2467 |
|
|
| 13 Aug 2013 08:36 PM |
| Oh did see the more than once. What aff said. |
|
|
| Report Abuse |
|
|
|
| 13 Aug 2013 08:36 PM |
I didn't give you an actual output it was an example.
And what does "Try testing more" mean?
I have a question on how to do something, not a broken script... |
|
|
| Report Abuse |
|
|
|
| 13 Aug 2013 08:36 PM |
Table={1,2,3,4,5,6}
for i,v in pairs(Table) do a = math.random(1,#Table) print(Table[a]) table.remove(Table,a) end |
|
|
| Report Abuse |
|
|
|
| 13 Aug 2013 08:37 PM |
Actually, I'd use a numerical for loop for that instead of a generic for loop.
Table={1,2,3,4,5,6}
for i = 1, #Table do e = math.random(1,#Table) print(e) table.remove(Table, e) end |
|
|
| Report Abuse |
|
|
magnalite
|
  |
| Joined: 18 Oct 2009 |
| Total Posts: 2467 |
|
| |
|
|
| 13 Aug 2013 08:37 PM |
| @Agent, thank you I guess that should have been blatantly obvious.... |
|
|
| Report Abuse |
|
|
| |
|
|
| 13 Aug 2013 08:38 PM |
| Wait why Numerical for instead of Generic for? Would it make a difference? I don't understand why it would |
|
|
| Report Abuse |
|
|
|
| 13 Aug 2013 08:39 PM |
| I'm not sure how the generic for loop will react when removing key-value pairs from the table. Using the numeric for loop would guarantee 6 iterations in your example. |
|
|
| Report Abuse |
|
|
|
| 13 Aug 2013 08:40 PM |
| Oh thats true... I'll test it and get back to you |
|
|
| Report Abuse |
|
|
Desperian
|
  |
| Joined: 07 Feb 2012 |
| Total Posts: 3371 |
|
|
| 13 Aug 2013 08:46 PM |
@AFF, Both of your given examples wouldn't work.
For example (Three tests of both):
Generic for loop: 3 1 1 || 1 1 3 || 3 2 2
Numerical for loop: 6 1 1 2 1 1 || 1 3 4 2 2 1 || 6 3 4 1 2 1
----
@OP,
Item={1,2,3,4,5,6}
repeat Rand = math.random(1, #Item) print(Item[Rand]) table.remove(Item, Rand) until #Item == 0 |
|
|
| Report Abuse |
|
|
|
| 13 Aug 2013 08:49 PM |
It would work I've been testing it.
Heres the two methods
local Table1 = {1,2,3,4,5,6,7,8,9,10} local Table2 = {11,12,13,14,15,16,17,18,19,20} print("===============================================") for i,v in pairs(Table1) do local e = math.random(1,#Table1) print(Table1[e]) table.remove(Table1,e) end print("----------------------------------------------") for i = 1, #Table2 do local b = math.random(1,#Table2) print(Table2[b]) table.remove(Table2,b) end
heres my output
=============================================== 1 2 5 4 3 ---------------------------------------------- 20 15 11 12 13 16 18 17 19 14 |
|
|
| Report Abuse |
|
|
MHebes
|
  |
| Joined: 04 Jan 2013 |
| Total Posts: 2278 |
|
|
| 13 Aug 2013 08:53 PM |
If you don't want the table to be blank (Only adding two lines :P):
local Table = {11,12,13,14,15,16,17,18,19,20} local copy = Table -- Here for i = 1, #Table do local b = math.random(1,#Table2) print(Table2[b]) table.remove(Table2,b) end Table = copy -- And here
~ Oh, I'm sorry, did I break your concentration? ~ |
|
|
| Report Abuse |
|
|
|
| 13 Aug 2013 08:56 PM |
@AFF: Dont you remove INDEXES and not VALUES from a table?
So when you table.remove(table, e), and e was the value, you should have done e = math.random(1, #table) print(table[e]) table.remove(table, e)? |
|
|
| Report Abuse |
|
|
|
| 13 Aug 2013 09:17 PM |
"@AFF, Both of your given examples wouldn't work. "
They did "work," but the printed value was the random index. Not the value. That said, if you change print(e) to print(Table[e]) then you will get the desired effect.
Apologies for not catching that. :/ |
|
|
| Report Abuse |
|
|
|
| 13 Aug 2013 09:18 PM |
| Oh yeah. I thought you had the value there, due to just print(e) :| |
|
|
| Report Abuse |
|
|
|
| 13 Aug 2013 09:19 PM |
"local Table = {11,12,13,14,15,16,17,18,19,20} local copy = Table -- Here for i = 1, #Table do local b = math.random(1,#Table2) print(Table2[b]) table.remove(Table2,b) end Table = copy -- And here"
That wouldn't work. You set 'copy' to the address of 'Table', meaning they both would point to the same table in memory. Both would be empty at the end because copy == Table. |
|
|
| Report Abuse |
|
|
| |
|
|
| 13 Aug 2013 09:22 PM |
Manually copy each key-value pair into a separate table. And sadly, there's no method in the table library to do this, either. |
|
|
| Report Abuse |
|
|
|
| 14 Aug 2013 01:29 PM |
Table = {1,2,3} Copy = {} for i = 1,#Table do table.insert(Copy,Table[i]) end
like that? |
|
|
| Report Abuse |
|
|
|
| 14 Aug 2013 01:32 PM |
For numeric-based key-value pairs, yes. This method works for all tables, though:
Table = {1, 2, 3} Copy = {} for _, v in pairs(Table) do rawset(Copy, _, v) end |
|
|
| Report Abuse |
|
|
| |
|