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 » Scripting Helpers
Home Search
 

Re: How do you do this RANDOM

Previous Thread :: Next Thread 
Clayguy247 is not online. Clayguy247
Joined: 03 Sep 2008
Total Posts: 3394
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 is not online. magnalite
Joined: 18 Oct 2009
Total Posts: 2467
13 Aug 2013 08:35 PM
Try testing more?
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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 is not online. 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
Clayguy247 is not online. Clayguy247
Joined: 03 Sep 2008
Total Posts: 3394
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
evolvedpikachu is not online. evolvedpikachu
Joined: 18 Aug 2010
Total Posts: 10696
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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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 is not online. magnalite
Joined: 18 Oct 2009
Total Posts: 2467
13 Aug 2013 08:37 PM
didn't*
Report Abuse
Clayguy247 is not online. Clayguy247
Joined: 03 Sep 2008
Total Posts: 3394
13 Aug 2013 08:37 PM
@Agent, thank you I guess that should have been blatantly obvious....
Report Abuse
evolvedpikachu is not online. evolvedpikachu
Joined: 18 Aug 2010
Total Posts: 10696
13 Aug 2013 08:37 PM
Dang it was late.
Report Abuse
Clayguy247 is not online. Clayguy247
Joined: 03 Sep 2008
Total Posts: 3394
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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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
Clayguy247 is not online. Clayguy247
Joined: 03 Sep 2008
Total Posts: 3394
13 Aug 2013 08:40 PM
Oh thats true... I'll test it and get back to you
Report Abuse
Desperian is not online. 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
Clayguy247 is not online. Clayguy247
Joined: 03 Sep 2008
Total Posts: 3394
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 is not online. 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
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
13 Aug 2013 09:18 PM
Oh yeah. I thought you had the value there, due to just print(e) :|
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
13 Aug 2013 09:20 PM
How DO you clone a table?
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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
Clayguy247 is not online. Clayguy247
Joined: 03 Sep 2008
Total Posts: 3394
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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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
Clayguy247 is not online. Clayguy247
Joined: 03 Sep 2008
Total Posts: 3394
14 Aug 2013 01:36 PM
What is "rawset"?
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • 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