• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Sorting equal numbers

Xikini

I whore myself out for likes
Senator
Premium User
Joined
Nov 17, 2010
Messages
6,811
Solutions
582
Reaction score
5,372
I'm trying to take a random string of numbers between 1-300, and sort them into roughly equal categories.

Below is the script I threw together to test.. and I'd use the random string created for the next execution.
It should be sorting the values from highest to lowest, and assigning the value to the current lowest number, which in theory.. should make the two teams fairly even.

For some reason though, I'd keep getting perfectly sorted teams, from a random assortment of numbers.
I expected numbers ranges in the green values, but not the pink / red values. Or at least, not as often as I'm seeing them.
Was I just getting unaturally lucky, or is there something wrong with how I'm sorting the the values?

Lua:
local count_1, team_1 = 0, {}
local count_2, team_2 = 0, {}
local numbers = {206,75,129,13,212,166,255,134,141,11,281,41,102,88,194,230,131,40,247,11,95,2,49,90,21,87,89,103,115,221,114,21,295,243,34,206,109,288,40,249}
table.sort(numbers)
for i = 1, #numbers do
   i = #numbers + 1 - i
   if count_1 <= count_2 then
      count_1 = count_1 + numbers[i]
      table.insert(team_1, numbers[i])
   else
      count_2 = count_2 + numbers[i]
      table.insert(team_2, numbers[i])
   end
end
print(count_1, count_2)

math.randomseed(os.time())
local txt = ""
for i = 1, 40 do
   txt = txt .. math.random(1, 300) .. ","
end
print(txt)
253,119,235,240,274,60,101,231,84,167,144,189,110,155,286,275,191,216,43,183,5,73,42,242,48,121,39,33,300,66,154,252,184,89,192,158,149,292,88,232
= 3171, 3144
202,53,282,219,250,214,211,45,162,40,290,29,69,271,254,92,168,217,257,62,93,199,73,198,80,206,82,266,284,242,225,185,294,207,104,244,120,14,288,282
= 3530 , 3543
211,236,75,30,52,48,154,264,281,7,286,48,94,25,73,26,44,291,255,187,300,90,33,261,139,32,187,186,10,146,97,221,81,172,250,133,219,103,96,199
= 2821, 2821
107,137,118,95,190,272,293,38,67,83,247,148,63,257,196,296,235,56,65,173,65,233,189,172,122,81,122,186,2,37,150,108,174,267,202,63,239,195,101,5
= 2924, 2925
297,134,288,42,277,97,214,213,6,28,201,223,277,175,249,145,2,171,19,291,80,143,265,199,220,60,263,55,261,142,118,257,275,105,299,252,202,213,165,207
= 3566, 3564
264,158,168,48,14,195,19,262,166,262,8,83,142,249,33,279,130,109,125,42,266,166,5,164,32,147,220,135,235,284,272,199,141,140,247,154,34,265,116,200
= 3089, 3089
146,110,204,181,173,127,234,78,272,6,183,59,239,207,139,249,197,135,300,237,226,88,134,215,81,251,258,278,238,89,185,84,198,88,264,71,215,198,148,187
= 3509, 3463
61,15,277,147,193,293,66,10,186,247,87,12,136,126,202,43,184,191,204,31,148,237,96,21,184,14,36,268,250,155,166,11,169,143,158,62,136,223,71,21
= 2640, 2640
298,226,82,167,96,102,193,104,77,205,87,269,113,37,207,112,271,90,55,240,80,93,172,176,260,250,115,252,142,253,229,140,179,10,6,274,112,198,77,189
= 3119, 3119
224,240,51,219,68,17,161,283,283,109,5,123,271,132,261,298,52,195,121,217,251,255,141,198,280,149,81,201,65,41,77,289,280,127,207,47,143,67,30,125
= 3194, 3190
206,75,129,13,212,166,255,134,141,11,281,41,102,88,194,230,131,40,247,11,95,2,49,90,21,87,89,103,115,221,114,21,295,243,34,206,109,288,40,249
= 2591, 2587
 
Tab:249,40,288,109,206,34,243,295,21,114,221,115,103,89,87,21,90,49,2,95,11,247,40,131,230,194,88,102,41,281,11,141,134,255,166,212,13,129,75,206,
Tab Sort:295,288,281,255,249,247,243,230,221,212,206,206,194,166,141,134,131,129,115,114,109,103,102,95,90,89,88,87,75,49,41,40,40,34,21,21,13,11,11,2,
add to count 1-295
add to count 2-288
add to count 2-281
add to count 1-255
add to count 1-249
add to count 2-247
add to count 1-243
add to count 2-230
add to count 1-221
add to count 2-212
add to count 2-206
add to count 1-206
add to count 2-194
add to count 1-166
add to count 1-141
add to count 2-134
add to count 1-131
add to count 2-129
add to count 1-115
add to count 2-114
add to count 1-109
add to count 2-103
add to count 1-102
add to count 2-95
add to count 1-90
add to count 2-89
add to count 2-88
add to count 1-87
add to count 1-75
add to count 2-49
add to count 2-41
add to count 1-40
add to count 2-40
add to count 1-34
add to count 2-21
add to count 1-21
add to count 2-13
add to count 2-11
add to count 1-11
add to count 2-2
2591 2587
 
Tab:249,40,288,109,206,34,243,295,21,114,221,115,103,89,87,21,90,49,2,95,11,247,40,131,230,194,88,102,41,281,11,141,134,255,166,212,13,129,75,206,
Tab Sort:295,288,281,255,249,247,243,230,221,212,206,206,194,166,141,134,131,129,115,114,109,103,102,95,90,89,88,87,75,49,41,40,40,34,21,21,13,11,11,2,
add to count 1-295
add to count 2-288
add to count 2-281
add to count 1-255
add to count 1-249
add to count 2-247
add to count 1-243
add to count 2-230
add to count 1-221
add to count 2-212
add to count 2-206
add to count 1-206
add to count 2-194
add to count 1-166
add to count 1-141
add to count 2-134
add to count 1-131
add to count 2-129
add to count 1-115
add to count 2-114
add to count 1-109
add to count 2-103
add to count 1-102
add to count 2-95
add to count 1-90
add to count 2-89
add to count 2-88
add to count 1-87
add to count 1-75
add to count 2-49
add to count 2-41
add to count 1-40
add to count 2-40
add to count 1-34
add to count 2-21
add to count 1-21
add to count 2-13
add to count 2-11
add to count 1-11
add to count 2-2
2591 2587
lol thanks, I know what it's doing.
It was just surprising to me, that a random set of numbers would get exactly equal results 4/11 times.
I guess that was my issue/question, was/is.. am I just getting extremely lucky with the numbers, or was my script not the best option for the job?

At this point, I think it was the latter. ¯\_(ツ)_/¯
 
Back
Top