|
| 12 Sep 2015 06:06 PM |
How would I get a random item from the table the way I have it setup (I knoew the 'Random69ner variable is all wrong.'?
table = { [69] = 1, [70] = 2, [71] = 3 }
local Random69ner = table[math.random(1,#table)] print(Random69ner[1].."|"..Random69ner[2]) |
|
|
| Report Abuse |
|
|
| |
|
|
| 12 Sep 2015 06:12 PM |
local Random = table[math.random(1,#Table)]
Returns one value from the table |
|
|
| Report Abuse |
|
|
| |
|
|
| 12 Sep 2015 06:14 PM |
| Because if you get one value, why are you trying to index [1] and [2] on your one value that isn't a table |
|
|
| Report Abuse |
|
|
|
| 12 Sep 2015 06:16 PM |
| 'I knoew the 'Random69ner variable is all wrong.' --you're suppose to read sir |
|
|
| Report Abuse |
|
|
|
| 12 Sep 2015 06:17 PM |
The variable isn't the problem, this is:
print(Random69ner[1].."|"..Random69ner[2]) |
|
|
| Report Abuse |
|
|
|
| 12 Sep 2015 06:17 PM |
..
Please understand I meant the whole line. |
|
|
| Report Abuse |
|
|
|
| 12 Sep 2015 06:19 PM |
| print(Random69ner[1].."|"..Random69ner[2]) should print like '69|1' and '70|2' and so on.. that's all i'm trying to do. Do you know how that can be possible? |
|
|
| Report Abuse |
|
|
Casualist
|
  |
| Joined: 26 Jun 2014 |
| Total Posts: 4443 |
|
|
| 12 Sep 2015 06:19 PM |
@OP OrangeKitten's post does not work because the len operator (#) is blind to non-consecutive and blind to non-integer indices.
The issue here is you is using non-consecutive indices (len starts from 1 and goes 2, 3, ... until it cannot find a number) i.e. print(#{[1] = 1, [2] = 2, [3] = 3, [5] = 4} == 3) --//3 of the 4 entries are consecutive starting from 1 > true
Use this code:
table = { [69] = 1, [70] = 2, [71] = 3 }
function randomEntry(tab) local temp = {} for key in pairs(tab) do temp[#temp+1] = key end return #tab == 0 and nil or tab[temp[math.random(1, #temp)]] end
print(randomEntry(table)) |
|
|
| Report Abuse |
|
|
|
| 12 Sep 2015 06:22 PM |
| @Casualist, it chose 69 and only printed 69.. |
|
|
| Report Abuse |
|
|
Casualist
|
  |
| Joined: 26 Jun 2014 |
| Total Posts: 4443 |
|
|
| 12 Sep 2015 06:23 PM |
| Post what you did, the code I posted works (I'm watching it print 1, 3, 2, 1, 2, 2, 3, 1, 1... in studio) |
|
|
| Report Abuse |
|
|
|
| 12 Sep 2015 06:24 PM |
I did this
table = { [69] = 1, [70] = 2, [71] = 3 }
function randomEntry(tab) local temp = {} for key in pairs(tab) do temp[#temp+1] = key end return #tab == 0 and nil or tab[temp[math.random(1, #temp)]] end
print(randomEntry(table)) |
|
|
| Report Abuse |
|
|
Casualist
|
  |
| Joined: 26 Jun 2014 |
| Total Posts: 4443 |
|
|
| 12 Sep 2015 06:25 PM |
Ignore me.
table = { {69, 1}; {70, 2}; {71, 3}; }
local Random69ner = table[math.random(1,#table)] print(Random69ner[1].."|"..Random69ner[2])
|
|
|
| Report Abuse |
|
|
|
| 12 Sep 2015 06:25 PM |
Yes, I did overlook that. But it also errors because of:
print(Random69ner[1].."|"..Random69ner[2]) |
|
|
| Report Abuse |
|
|
|
| 12 Sep 2015 06:25 PM |
local randomKey = function(tab) local i = {} for k,q in pairs(tab) do table.insert(i,k) end return tab[i[math.random(#i)]] end
local tab = {Hi = "Hello",Ok = "Okay",No = "nada"} local val = randomKey(tab) print(val) |
|
|
| Report Abuse |
|
|
|
| 12 Sep 2015 06:26 PM |
| I was hoping it could print like '69|1' and '70|2' and so on. |
|
|
| Report Abuse |
|
|
| |
|
Casualist
|
  |
| Joined: 26 Jun 2014 |
| Total Posts: 4443 |
|
|
| 12 Sep 2015 06:33 PM |
Ignore my first set of posts. I kinda skimmed it and was in the same mindset from a previous thread that did something similar.
This should do what you want:
table = { {69, 1}; {70, 2}; {71, 3}; }
local Random69ner = table[math.random(1,#table)] print(Random69ner[1].."|"..Random69ner[2]) |
|
|
| Report Abuse |
|
|
| |
|
| |
|