generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: math.random on tables

Previous Thread :: Next Thread 
BloodLitch101 is not online. BloodLitch101
Joined: 13 Jul 2011
Total Posts: 941
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
BloodLitch101 is not online. BloodLitch101
Joined: 13 Jul 2011
Total Posts: 941
12 Sep 2015 06:09 PM
b1
Report Abuse
OrangeKitten is not online. OrangeKitten
Joined: 12 Jul 2007
Total Posts: 383
12 Sep 2015 06:12 PM
local Random = table[math.random(1,#Table)]

Returns one value from the table
Report Abuse
BloodLitch101 is not online. BloodLitch101
Joined: 13 Jul 2011
Total Posts: 941
12 Sep 2015 06:13 PM
It errors though sir.
Report Abuse
OrangeKitten is not online. OrangeKitten
Joined: 12 Jul 2007
Total Posts: 383
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
BloodLitch101 is not online. BloodLitch101
Joined: 13 Jul 2011
Total Posts: 941
12 Sep 2015 06:16 PM
'I knoew the 'Random69ner variable is all wrong.' --you're suppose to read sir
Report Abuse
OrangeKitten is not online. OrangeKitten
Joined: 12 Jul 2007
Total Posts: 383
12 Sep 2015 06:17 PM
The variable isn't the problem, this is:

print(Random69ner[1].."|"..Random69ner[2])
Report Abuse
BloodLitch101 is not online. BloodLitch101
Joined: 13 Jul 2011
Total Posts: 941
12 Sep 2015 06:17 PM
..

Please understand I meant the whole line.
Report Abuse
BloodLitch101 is not online. BloodLitch101
Joined: 13 Jul 2011
Total Posts: 941
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 is not online. 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
BloodLitch101 is not online. BloodLitch101
Joined: 13 Jul 2011
Total Posts: 941
12 Sep 2015 06:22 PM
@Casualist, it chose 69 and only printed 69..
Report Abuse
Casualist is not online. 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
BloodLitch101 is not online. BloodLitch101
Joined: 13 Jul 2011
Total Posts: 941
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 is not online. 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
OrangeKitten is not online. OrangeKitten
Joined: 12 Jul 2007
Total Posts: 383
12 Sep 2015 06:25 PM
Yes, I did overlook that. But it also errors because of:

print(Random69ner[1].."|"..Random69ner[2])
Report Abuse
morashsPeasant is not online. morashsPeasant
Joined: 06 Jan 2011
Total Posts: 4944
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
BloodLitch101 is not online. BloodLitch101
Joined: 13 Jul 2011
Total Posts: 941
12 Sep 2015 06:26 PM
I was hoping it could print like '69|1' and '70|2' and so on.
Report Abuse
BloodLitch101 is not online. BloodLitch101
Joined: 13 Jul 2011
Total Posts: 941
12 Sep 2015 06:31 PM
@Casualist?
Report Abuse
Casualist is not online. 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
morashsPeasant is not online. morashsPeasant
Joined: 06 Jan 2011
Total Posts: 4944
12 Sep 2015 06:33 PM
cas is better
Report Abuse
BloodLitch101 is not online. BloodLitch101
Joined: 13 Jul 2011
Total Posts: 941
12 Sep 2015 06:34 PM
IT WORKED! thanks kid.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image