# Math Random? Giving guns by random?

Hello.
So I am currently making a gamemode, and i’m new to LUA and I have a question on how I would go about something.
I need to make it so each round it will choose let’s say 1 - 5 for each team (4 teams), and it would give a gun based on the number you get.
I assume you need to use math random and I have a vague idea of this.
Wouldn’t it be something like…
if number == 1 then
ply:Give(“weapon_shotgun”)
elseif number == 2 then
ply:Give(“weapon_357”)

If it is like that I just need help with math random.
Can someone point me in the right direction?

local number = math.random(5)

gives you an integer from 1-5

Hmm, okay now that I know that I just realized that I don’t know how i’d really incorporate that into my code.
My current code is:
[lua]

include( ‘shared.lua’ )

function GM:PlayerInitialSpawn( ply )
ply:SetTeam( 5 )
end

``````ply:StripWeapons()

if ply:Team() == 1 then
ply:Give( "weapon_physcannon" )

elseif ply:Team() == 2 then
ply:Give( "weapon_physcannon" )

elseif ply:Team() == 3 then
ply:Give( "weapons_physcannon" )

elseif ply:Team() == 4 then
ply:Give( "weapons_physcannon" )
end
``````

end

function team_1( ply )
ply:SetTeam( 1 )
ply:Spawn()
ply:PrintMessage(HUD_PRINTCENTER, “You have joined Team 1!”)
end

function team_2( ply )
ply:SetTeam( 2 )
ply:Spawn()
ply:PrintMessage(HUD_PRINTCENTER, “You have joined Team 2!”)
end

function team_3( ply )
ply:SetTeam( 3 )
ply:Spawn()
ply:PrintMessage(HUD_PRINTCENTER, “You have joined Team 3!”)
end

function team_4( ply )
ply:SetTeam( 4 )
ply:Spawn()
ply:PrintMessage(HUD_PRINTCENTER, “You have joined Team 4!”)
end

[/lua]
Basically with local number = math.random(2) I want to go…
if number == 1 then
ply:Give(“weapon_shotgun”)
elseif number == 2 then
ply:Give(“weapon_357”)

But where would I put that if I want my team to randomly pick a number between 1 and 2? My first thought would have been GM:PlayerLoadout but for some reason it didn’t make sense. I would have to make a function out of this but could someone clear this up?

[editline]7th December 2012[/editline]

Alright I looked around a bit and I think I got something.
Would this work?
[lua]
function randomGunSpawn()
if number == 1 then
ply:Give(“weapon_shotgun”)
elseif number == 2 then
ply:Give(“weapon_357”)
end
end

[/lua]

[editline]7th December 2012[/editline]

Nevermind, I fixed it!
That is a great feeling when you are new to scripting.
[lua]
function GM:PlayerSpawn( ply )
local number = math.random(2)
if number == 1 then
ply:Give(“weapon_shotgun”)
elseif number == 2 then
ply:Give(“weapon_357”)
end
end
[/lua] was all it took!

[lua]local guns = { “weapon_shotgun”, “weapon_357” }
function randomGunSpawn( ply )
ply:Give( guns[math.random(#guns)] )
end[/lua]

Why do would you use math.random for that ?
Just use ply:Give( table.Random( guns ) ).

table.Random does the same thing so it doesn’t even matter.

Yea but table.Random is less code than the method with math.random and does the same.

In the end no, it’s not. When he said it did the exact same thing he meant it. You’d actually end up with 1 more function call that way.

A better argument for table.Random is to reduce redundancy in the code.

This being said the impact of optimizing one way or another is so small that’s it’s not worth arguing over.