Question about math.random

Here is the short version of the function:

[lua]

function GM:PlayerSpawn(ply)

if ply:IsUserGroup("admin") then
	ply:Give( "weapon_ttt_M3" )
	ply:Give( "weapon_zm_revolver" )
	ply:GiveAmmo( 24, "Buckshot" )
	ply:GiveAmmo( 28, "alyxgun" )

else
	ply:Give( "weapon_zm_pistol" )
	ply:GiveAmmo( 20, "Pistol" )

end
end
[/lua]

For the “else” i want to add a few different pistols and have it pick a pistol at random. If i was unclear please let me know, any input on this would be appreciated!

It would be better if you added each pistol to a table, and then used table.Random. Here’s an example:
[lua]Pistols = {“Deagle”, “P228”, “Glock”}

function GM:PlayerLoadout(ply)
ply:Give(table.Random(Pistols))
end[/lua]

awesome it worked great tyvm!

Do you mean it won’t pick a random pistol, rather give them the same one each time?

I will have to do some more testing I am not 100% sure yet. It is giving a random pistol each round but I’m not sure if its giving the same random pistol to each player when the round starts.

Do this
[lua]
Pistols = {“Deagle”, “P228”, “Glock”}

function GM:PlayerLoadout(ply)
ply:Give(Pistols[math.Random(1, #Pistols)])
end
[/lua]

I will give that a shot when i get home from work, tyvm

Whats the difference?

Cunts is faster by a few miliseconds, I don’t see why it matters.

toasters did not work but I don’t have to worry about it c-units is working, i tested it for myself this afternoon. Is this how I would give item ammo for each weapon that uses a different ammo type?

[lua]
Pistols = {“weapon_zm_revolver, item_ammo_revolver_ttt”, “weapon_ttt_glock, item_ammo_pistol_ttt”}

function GM:PlayerLoadout(ply)
ply:Give(Pistols[math.Random(1, #Pistols)])
end

[/lua]

They do the exact same thing, how would one work and not the other.

[lua]
local Pistols = {
{“weapon_zm_revolver”,“SMG1”,12},
{“item_ammo_revolver_ttt”,“AR2”,12},
}

function GM:PlayerLoadout(ply)
local randomNum = math.random(1,#Pistols);

ply:Give(Pistols[randNum][1]);
ply:GiveAmmo(Pistols[randNum][3],Pistols[randNum][2]);

end
[/lua]

Zack quit trying to show off, also table.Random is weird, I’m not sure why but whenever you call it, it gets the exact same “random” value from the table but when you use math.Random it doesn’t seem to have this issue.

[lua]

function table.Random (t)

local rk = math.random( 1, table.Count( t ) )
local i = 1
for k, v in pairs(t) do
if ( i == rk ) then return v end
i = i + 1
end

end
[/lua]

table.Random essentially does the same exact thing…

That’s because of the fact math.random is not as random as you would hope

I love you.

It seems the other way around as math.Random isn’t a function. And toaster didn’t post any code.