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: Randomizing the order of a table?

Previous Thread :: Next Thread 
LordDracius is online. LordDracius
Joined: 22 Oct 2011
Total Posts: 1056
23 May 2017 03:13 PM
For example, I have a table:

local players = {player1, player2, player3}

How do I make the table shuffle?
Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
23 May 2017 03:14 PM
local function shuffle( tbl )
local new = {}
for _,i in ipairs(tbl) do
table.insert(new, math.random(2), i)
end
return new
end
Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
23 May 2017 03:16 PM
oops i mean
table.insert(new,math.random(#new+1),i)
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
23 May 2017 03:24 PM
Wow that's disgusting, you took an O(n) operation and made it O(n^2)
Nice.
Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
23 May 2017 03:26 PM
lol idk i knew how to shuffle arrays but then i forgot so i made this thing
what was the most efficient method again w/o removing random entries?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
23 May 2017 03:28 PM
If you don't care about preserving the state of the current table, literally just

for i = 1, n do
local j = math.random(n)
tbl[i], tbl[j] = tbl[j], tbl[i]
end

Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
23 May 2017 03:31 PM
thx

ok then so

local function shuffle( tbl )
for i = 1, #tbl do
local j = math.random(n)
tbl[i], tbl[j] = tbl[j], tbl[i]
end
return tbl
end

can either do

local new = shuffle( myArray ) --in the case that myArray is anonymous or u want to do things in 1 step

or

local myArray = myArray
shuffle(myArray)
print(unpack(myArray)) --now shuffled
Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
23 May 2017 03:34 PM
table.sort(Table, ############ ### local decide = math.random() if decide < .5 then return false else return true end end)
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
23 May 2017 03:36 PM
That's even worse lol, like WAAAAY worse.
Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
23 May 2017 03:36 PM
*
function()
local decide = math.random()
if decide < .5 then
return false
else
return true
end
end)


Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
23 May 2017 03:37 PM
#### explain, why so?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
23 May 2017 03:40 PM
Because you rely on RANDOM values to whether or not you should stop sorting, meaning theoretically it can literally take forever to sort the array
Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
23 May 2017 03:53 PM
I tried running a test with table.sort() and jeez, only now I figured that it can only be ## # ## or ## # ### GAWD -_-
Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
23 May 2017 04:02 PM
Aight so, I created my own Sort() function that works similarly to table.sort but in my own way. The result was: Numerical sort test finished in 0.0034348964691162 seconds. Sort test finished in 0.006040096282959 seconds. Numerical sort was faster by 0.0026051998138428 seconds. So ur method, #### is nearly twice as fast, but I was just pointing at a different possibility since u already called out the numerical method (The one u posted), and I do get that it would be better to swap them directly, but still, it's not like mine doesn't follow the title ;)
Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
23 May 2017 04:18 PM
It's just array shuffling. Calm down. If you don't know the size of the array then start ##### loop at 2. There's no reason to shuffle an array that has only one value. Either way his method is about as efficient as it gets.
Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
23 May 2017 04:55 PM
Not only that but KapKing47 could return an invalid sorting order I believe.
Report Abuse
j1my3p1x is not online. j1my3p1x
Joined: 16 Jan 2010
Total Posts: 978
23 May 2017 04:56 PM
im pretty sure that the random table.sort method is bad statistically. the last index of the array shouldn't move to the front very often
Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
23 May 2017 04:59 PM
Alright so its bad because

1.It randomizes too much when it does work, with the theoretical chance of doing so forever

2.It has a good chance to error, the larger the table the larger the chance (I tested it and was right; invalid sorting function a bunch of times when I called it like 10 times)

3.It has a bad chance of even switching things in a way you would like
Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
23 May 2017 05:22 PM
1. I would actually say it doesn't randomize enough, I'll explain in a sec. 2. Yeah I know, that's why I made my own sort function. 3. Yeah I know, at first I didn't think about it, till about 10 minutes after posting about it I thought it was decent, but then realized it would only swap most of the values to # ###### and a very small chance to do 2 places and even smaller for 3 places, so there is not enough randomization. But this wasn't such a waste of time for me, I use all this to learn for the future, so I'm actually glad I did this test.
Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
23 May 2017 05:25 PM
You can get better randomization with less value swapping so no you are wrong when you say it doesn't randomize enough, it doesn't need to randomize more it needs to randomize better.

If you made your own sort function just for this, thats a terrible idea just use a shuffle function like what cntkillme provided.

If you made your own sort function for use with other ideas, thats also a terrible idea because I'm sure its much less efficient than the built in one.
Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
24 May 2017 07:08 AM
1. I never said that it needs to swap more, I just said that it doesn't randomize it well cos the chances are that most values that are swapped will only be swapped to # ###### ## ### read what I said before; I said that I only posted what I thought was cos cntk already posted that method I could have posted, so I was just trying to think of more methods (It can be interesting to learn about different methods sometimes, no joke). 3. And as I said, I only made the sort function to work with what I tried to do, cos in the table.sort() function u HAD to use ## # ## or ## # ## (Or the opposite way around) there was just no way to randomize it, I even tried adding random values to both of them, and yet again, it errored for me, I had no choice.
Report Abuse
Dankvisky is not online. Dankvisky
Joined: 04 Mar 2012
Total Posts: 5430
24 May 2017 08:40 AM
I had the same question:

forum.roblox.com/Forum/ShowPost.aspx?PostID=212988478


Report Abuse
samy22 is not online. samy22
Joined: 28 Sep 2008
Total Posts: 2181
24 May 2017 09:29 AM
Welding yourself to the platform just sticks you to it. It wouldn't allow movement on the platform.


Not sure, but changing friction might help in custom physical properties. Technically, if the friction is strong enough irl and you are on a moving platform then you won't fall off. Check to see if this same concept works in roblox by increasing friction of your feet/the platform or both.
Report Abuse
samy22 is not online. samy22
Joined: 28 Sep 2008
Total Posts: 2181
24 May 2017 09:29 AM
Woops, wrong thread.
Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
24 May 2017 12:43 PM
cant u also just iterate through the table via next or local i = # ### ### in next,tbl do tlb[k],tbl[i]=tbl[i],v end return tbl
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