# Help with math.Random

Hey, people.

I have just started on a new and awesome game mode, but I don’t know how to make this:
I need a function to check if there are more than 5 players on the server, and if there are, pick 1 person for each 5 people on the server to join a certain team.

So far I got this function:
[lua]
function EnoughPlayers()
if #player.GetAll() => 5 then

``````end
``````

end
[/lua]

math.random not math.Random

[lua]
function EnoughPlayers()
if player.GetAll() > 4 then
local ply = table.Random(player.GetAll())
print("Random Player Nick: "…ply:Nick())
end
end
[/lua]

Thanks a lot, man.
But now I’m stuck with another problem - How to make it have:

2 picked players if there are more than 9 total players
3 picked players if there are more than 14 total players
4 picked players if there are more than 19 total players

and so on, because I can’t seem to figure it out.

^ More than 9, 14, and 19 players you mean.

[lua]
function EnoughPlayers()
local numplayers = player.GetAll()
local scummers = math.Round(0.2 * numplayers)

for q=1,scummers do

``````	local ply = table.Random(player.GetAll())
print("Random Player #"..q..":"..ply:Nick())
``````

end
end
[/lua]

Sorry if there are some spelling errors there but I think that is the jist of what you want.

Will that pick 2 players at the same time?
Basically, what I am trying to do is

Pick 1 player if there are more than 5 total players
Pick 2 players if there are more than 10 total players
Pick 3 players if there are more than 15 total players

And so on.

This is what I have so far:
[lua]
function EnoughPlayers()
local Everyone = player.GetAll()
local LuckyBastards = math.Round(0.2 * Everyone)
for q=1,LuckyBastards do
local ply = table.Random(player.GetAll())
ply:Team() == 2
ply:PrintMessage(HUD_PRINTTALK, "You are a Wobble Bastard, run and hide before the hunters get to you!
")
else
ply:Team() == 1
ply:PrintMessage(HUD_PRINTTALK, "You are a Hunter, eliminate every Wobble Bastard before time runs out!
")
end
end
[/lua]

From What I’ve understood about that code is. LuckyBastards = 0.2 * 5 (1), for q=1 (No idea what this is), 1 do -----BLARGH----- end end

So I’m sure it will work.

All I want is for it to pick 1 random player for EACH 5 players.
This is to make it simple to understand:

Total | Bastards
1-4 | 0
5-9 | 1
10-14 | 2
15-19 | 3
20-24 | 4
25-29 | 5
30-34 | 6
35-40 | 7

Just like in TTT, where there is 1 traitor for each 4 players and 1 detective for every 8, except in this game mode the round does not start unless there are 5 people in play.

[lua]local NotBastards = player.GetAll()
local Bastards = {}

– Calculate how many lucky bastards you’ll have
local NumBastards = math.Round(#NotBastards * 0.2)

– Now you’re going to pick random players from the NotBastards table and transfer them to the Bastards table
for i=1, NumBastards do
– table.remove(t, i) removes the element number i from the table t, and returns the element removed
– table.insert(t, e) adds an element e to the end of the table t
– math.random(l,u) returns a random integer between l and u, including l and u
– This will therefore pick a random player in the NotBastards table, remove him from that table, and add it to the Bastards table
table.insert(Bastards, table.remove(NotBastards, math.random(1, #NotBastards)))
end

– At this moment, you know have all hunters in NotBastards, and all lucky bastards in Bastards
– “for _,v in pairs(t) do” is the most straightforward way to do something for all v elements in a table t
for _,v in pairs(NotBastards) do
– setting team, showing little message
v:SetTeam(1)
v:PrintMessage(HUD_PRINTTALK, “You are a Hunter, eliminate every Wobble Bastards before time runs out!”)
end

for _,v in pairs(Bastards) do
– same here
v:SetTeam(2)
v:PrintMessage(HUD_PRINTTALK, “You are a Wobble Bastard, run and hide before the hunters get to you!”)
end[/lua]

And that’s how you can do it. There are many reasons why your code won’t work, but the main problem is that you’re thinking it wrong. Picking a random player every iteration that way could pick the same player several times. It would happen more often than you think.

Here, at the beginning, you have a list of all players, NotBastards, and an empty list, Bastards.

NumBastards is the number of lucky bastards you’ll have, DO NOT FORGET the #, I’ve seen you guys forget it all the time. NotBastards is the list. #NotBastards is the number of elements in that list.

Now you’re going to pick a lucky bastard, NumBastards times.
Every time, you pick a random player from NotBastards, remove him from that table, and add him to the Bastards table. That makes sure than once a player is selected, he won’t be selected again. Then you do it again, and again, and the NotBastards table will gradually get smaller as the Bastards table grows.

Once you’re done, you have all hunters in NotBastards, and all bastards in Bastards. You can then iterate through those two table to assign a team to their players, and show a little message thing.

Also you should really take some time to read this: http://www.lua.org/pil/
Before learning how to use the functions that GMod provide, it’s always good to actually master the language. You know there’s something wrong when you write things such as ply:Team() == 2, which makes no sense at all.

Coolio.

Thanks for helping me out.
I will test it when I get the chance, but for now I will try to learn Lua a little bit more.
The “ply:Team() == 2” would only be correct if it’s after the if statement. I accidentally put it there. Oops.