Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to get more random responses? [closed]

Asked by 8 years ago
local players = game.Players:GetChildren()
local teams = {"Persimmon", "Cyan"}
local count = 1
for i, v in pairs(players) do
    v.TeamColor = BrickColor.new(teams[count])
        count = count + 1
    v.PlayerFolder:WaitForChild("Team").Value = tostring(v.TeamColor)
    if count > #teams then
        count = 1
    end
end

The whole point of this script is to get random teams for every player. The only problem I have noticed is that every round I'm on the exact same team. Can anyone please help?

0
[disregard i misunderstood] BlueTaslem 18071 — 8y
1
I mean that every single time I get the same team. When I have 3 people on I tried for 10 rounds, and all 3 of us got the exact same team NinjoOnline 1146 — 8y
0
use math.randomseed to set a new seed, ideally the result of tick() lukeb50 631 — 8y

Locked by OldPalHappy

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

2 answers

Log in to vote
6
Answered by 8 years ago
Edited 7 years ago

Of course this isn't random. You're not using a single random function.

I think what you're trying to do is create separate teams with as equal number of players as possible. Because you're already doing this, I have an idea that will compliment with your method, without changing anything.

Just create a table with the players inside, but shuffled. This will create a table of players randomly indexed.

Below is the added code which does this, combined with your code.

local players = game.Players:GetPlayers() -- I changed this to GetPlayers instead of GetChildren
local teams = {"Persimmon", "Cyan"}
local count = 1

for i = #players, 2, -1 do
    local j = math.random (i)
    players[i], players[j] = players[j], players[i]
end

for i, v in pairs(players) do
    v.TeamColor = BrickColor.new(teams[count])
    count = count + 1
    v.PlayerFolder:WaitForChild("Team").Value = tostring(v.TeamColor)

    if count > #teams then
        count = 1
    end
end

I haven't tested this. If there is anything wrong, let me know.

More Info.

0
You know, instead of saying iterations, you can just do `for i = #players...`. It's shorter than putting iterations anyway. EzraNehemiah_TF2 3552 — 8y
0
Yep, edited. OldPalHappy 1477 — 8y
Ad
Log in to vote
1
Answered by
doomiiii 112
8 years ago
Edited 8 years ago

What you want is constrained random assignment:

1) The team color of each player should be random

2) At the same time, you have certain constraints, e.g. you want all teams to have about an equal amount of players

The script below satisfies both requirements. Note that LUA random implementation seems to not be "very random", but adding a bunch of extra calls to math.random() seems to help (see explanation below).

local teams = {"Persimmon", "Cyan"}

-- initialize team counts
local counts = {}
for i=1, #teams do counts[i]=0 end

-- determine how many players per team (at most)
local maxCount = math.ceil(#players / #teams)

-- re-randomize every time a game starts 
-- NOTE: because Lua random number implementation seems to be very weak, adding a bunch of extra calls to random() can help things a bit
-- reference: http://stackoverflow.com/questions/20154991/generating-uniform-random-numbers-in-lua
math.randomseed(tick())
math.random(); math.random(); math.random()

-- assign pseudo-random team to each player
for i, v in pairs(players) do
  -- random team index: keep rolling the dice until we get a "good" team
  local teamIndex
  repeat
    teamIndex = 1 + math.floor(math.random() * #teams)
    math.random()
    math.random()
    math.random()
    math.random()
  until counts[teamIndex] < maxCount

  -- update team player count
  counts[teamIndex] = counts[teamIndex]+1 

  -- get color from index
    local teamColor = teams[teamIndex]

  print(teamColor)

  -- use teamColor here    
end