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: Randomizing a table

Previous Thread :: Next Thread 
crazyman32 is online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
12 Dec 2011 05:45 AM
Ok, so I need to randomize a table into two separate tables (equally). I have the code below to do so, but is there a better way?

My way:


local t = {1,2,3,4,5,6,7,8,9}
local t0,t1 = {},{}

local x = 0
while (#t > 0) do
x = (x == 0 and 1 or 0)
local i = math.random(#t)
table.insert((x == 0 and t0 or t1),t[i])
table.remove(t,i)
end
Report Abuse
nate890 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
12 Dec 2011 07:11 AM
err :S

local t={"One","Two","Three","Four","Five"}
local t0,t1,usd={},{},{}

function findTableObj(table,key)
    for index,child in pairs(table) do
        if child==key then
            return index
        end
    end
end

while #t0+#t1~=#t and wait() do
    local num=math.random(0,#t)
    if not findTableObj(usd,num) then
        table.insert(usd,num)
        table.insert(#t0>#t1 and t1 or t0,t[num])
    end
end

­<'+1 Post. Ujelly?'>
Report Abuse
nate890 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
12 Dec 2011 07:11 AM
Y u no auto tab

        local t={"One","Two","Three","Four","Five"}
    local t0,t1,usd={},{},{}

    function findTableObj(table,key)
        for index,child in pairs(table) do
            if child==key then
                return index
            end
        end
    end

    while #t0+#t1~=#t and wait() do
        local num=math.random(0,#t)
        if not findTableObj(usd,num) then
            table.insert(usd,num)
            table.insert(#t0>#t1 and t1 or t0,t[num])
        end
    end

­<'+1 Post. Ujelly?'>
Report Abuse
nate890 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
12 Dec 2011 11:51 AM
On lunch for school :O
Or you can do this

    local t={"One","Two","Three","Four","Five"}
    local t0,t1,usd={},{},{}

    while #t0+#t1~=#t do
        local num=math.random(0,#t)
        if not usd[num] then
            table.insert(usd,num,num)
            table.insert(#t0>#t1 and t1 or t0,t[num])
        end
    end

instead of having the "findTableObj" function

­<'+1 Post. Ujelly?'>
Report Abuse
nate890 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
12 Dec 2011 12:27 PM
Or you can do something like this, which is very similar to how you did it

    local t={"One","Two","Three","Four","Five"}
    local t0,t1={},{}

    function sortTable(tableFrom,tableTo,tableTo2)
        local amnt=#tableFrom
        while #tableTo+#tableTo2~=amnt and wait() do
            local num=math.random(0,#t)
            table.insert(#tableTo>#tableTo2 and tableTo2 or tableTo,tableFrom[num])
            table.remove(tableFrom,num)
        end
    end

­<'+1 Post. Ujelly?'>
Report Abuse
nate890 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
12 Dec 2011 12:30 PM
OOOORR, You can do it without an added variable (I have nothing better to do but post)

    local t={"One","Two","Three","Four","Five"}
    local t0,t1={},{}

    function sortTable(tableFrom,tableTo,tableTo2)
        while #t~=0 and wait() do
            local num=math.random(0,#t)
            table.insert(#tableTo>#tableTo2 and tableTo2 or tableTo,tableFrom[num])
            table.remove(tableFrom,num)
        end
    end


­<'+1 Post. Ujelly?'>
Report Abuse
Miksterman is not online. Miksterman
Joined: 28 Dec 2008
Total Posts: 397
12 Dec 2011 02:00 PM
Or you could have it return the tables.

Table = {1, 2, 3, 4, 5, 6, 7, 8}

function SortTable(T)
N = math.ceil(#T/2)
T1, T2 = {}, {}
for Num=1,N do
Ran = math.random(#T)
table.insert(T1, T[Ran])
table.remove(T, Ran)
end
T2 = T
return T1, T2)
end

Your is probably better I just haven't scripted in a while and needed something to quickly code....
Report Abuse
blockoo is not online. blockoo
Joined: 08 Nov 2007
Total Posts: 17202
12 Dec 2011 02:48 PM
Lol, everyone is making this way too difficult...

start = {"A", "B", "C", "D"}
a, b = {}, {}
last = "b"
for i = 1, #start do
if (last == "b") then
last = "a"
local rand = math.random(1, #start)
table.insert(a, start[rand])
table.remove(start, rand)
else
last = "b"
local rand = math.random(1, #start)
table.insert(b, start[rand])
table.remove(start, rand)
end
end
Report Abuse
pwnedu46 is not online. pwnedu46
Joined: 23 May 2009
Total Posts: 7534
12 Dec 2011 03:07 PM
    local tbl = {1, 2, 3, 4, 5, 6, 7, 8, 9 }
    local a, b = {}, {}
    
    for i, v in pairs(tbl) do
        if i%2 == 1 then
            table.insert(a, v)
        else
            table.insert(b, v)
        end
        tbl = {}
    end


----------
    You should leave now...
Report Abuse
pwnedu46 is not online. pwnedu46
Joined: 23 May 2009
Total Posts: 7534
12 Dec 2011 03:07 PM
Oh...I didn't see the randomize part. Ignore my last post...


----------
    You should leave now...
Report Abuse
meteorit210 is not online. meteorit210
Joined: 08 Sep 2010
Total Posts: 348
12 Dec 2011 03:56 PM
t0 = {0,1,2,3,4,5,6,7,8,9}
t1 = {}
t2 = {}
for i = 1,5 do
t1[i] = t0[math.random(1,10)]
end
for x = 1,5 do
t2[x] = t0[math.random(1,10)]
end


That what you want?



Report Abuse
crazyman32 is online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
12 Dec 2011 03:58 PM
Meh, I'm just gonna use mine.

@meteorit - It needs to randomly sort them, but keep it even (obviously offset of 1 if odd number)
Report Abuse
Varp is not online. Varp
Joined: 18 Nov 2009
Total Posts: 5333
12 Dec 2011 04:24 PM
The fastest way I can think of is:

function randomize(t)
local t0,t1 = {},{}
local n = #t --n will always be the number of unassigned values.
local r = math.floor(n/2) --r will be the number still required in t0
for i = 1,#t do --iterate over all values in t.
if(math.random(1,n) <= r)then --There is a r/n chance of placing a value in t0
table.insert(t0,t[i])
r = r - 1 --We have put a value in t0, so we don't need as many more.
else
table.insert(t1,t[i])
end
n = n - 1 --We assigned a value.
end
return t0,t1
end

Then an example usage:

local t = {1,2,3,4,5,6,7,8}
local t0,t1 = randomize(t)
for i = 1,#t0 do print(t0[i]) end
print("---")
for i = 1,#t1 do print(t1[i]) end

Report Abuse
jack48271 is not online. jack48271
Joined: 16 Jan 2009
Total Posts: 2608
12 Dec 2011 04:50 PM
I never randomize my tables probably because i'm too lazy XD just find the table more organized.
Report Abuse
nate890 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
12 Dec 2011 05:07 PM
Eh. my last method wasn't that bad. Was similar to yours but it doesn't use no variable like 'x'

­<'+1 Post. Ujelly?'>
Report Abuse
scripterzack is not online. scripterzack
Joined: 22 Jan 2011
Total Posts: 562
12 Dec 2011 05:17 PM
easy =)

function SplitTable(tab)
local t1,t2={},{}
local left=tab--items left to randamize
while #left>0 do
local n=math.random(1,#left)
table.insert(t1,left[n])
table.remove(left,n)
if #left>0 then
local n=math.random(1,#left)
table.insert(t2,left[n])
table.remove(left,n)
end
end
return t1,t2
end
Report Abuse
scripterzack is not online. scripterzack
Joined: 22 Jan 2011
Total Posts: 562
12 Dec 2011 05:17 PM
Probaly not shorter but the only other way i could think of =D
Report Abuse
crazyman32 is online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
12 Dec 2011 05:18 PM
So far mine is still shorter, and in my eye more efficient, than the ones people have posted, so I'm just going to use mine since it works fine :P
Report Abuse
crazypotato4 is not online. crazypotato4
Joined: 22 Feb 2010
Total Posts: 20000
12 Dec 2011 05:27 PM
local t0 = {1,2,3,4,5,6,7,8,9}
local t1 = {}

for i = 1, #t0/2 do
local r = math.random(#t0)
table.insert(t1, t0[r])
table.remove(t0, r)
end


do i win
Report Abuse
nate890 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
12 Dec 2011 08:06 PM
mines more short ​>:O

­<'+1 Post. Ujelly?'>
Report Abuse
blockoo is not online. blockoo
Joined: 08 Nov 2007
Total Posts: 17202
12 Dec 2011 08:44 PM
@crazy
Note that CM32 has TWO tables...
Report Abuse
crazyman32 is online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
12 Dec 2011 10:11 PM
crazypotatos would still work for me though, since the original table would just be left for rotting anyway.
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
12 Dec 2011 10:46 PM
I remember I had to make one for a special maze generator I was making. Dunno where I put it tho :/
Report Abuse
oxcool1 is not online. oxcool1
Joined: 05 Nov 2009
Total Posts: 15444
13 Dec 2011 02:50 AM
[ Content Deleted ]
Report Abuse
crazyman32 is online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
13 Dec 2011 05:39 AM
Nope, no one won :P
I'm just going to use mine.
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