Forum

> > CS2D > Scripts > Pick a player at random
Forums overviewCS2D overview Scripts overviewLog in to reply

English Pick a player at random

28 replies
Page
To the start Previous 1 2 Next To the start

old Pick a player at random

DanielG
User Off Offline

Quote
How would I "pick a player at random". I know I would use random.math(1,32), but how would I make it pick the player ID number of a person on the server? Thanks in advance.

old Re: Pick a player at random

Flacko
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
nemesis = 0
function randomplayer()
	local tt = player(0,"team1")
	local ct = player(0,"team2")
	local id = math.rand(1,#tt + #ct)
	return tt[id] or ct[id]
end

addhook("startround","_startround")
function _startround()
	math.randomseed(os.time()) --seed every once in a while
	nemesis = randomplayer() --get the new 'nemesis'
	parse("equip "..nemesis.." 45") --give the player a laser
end
edited 1×, last 22.05.11 10:43:20 pm

old Re: Pick a player at random

ohaz
User Off Offline

Quote
correction:
1
2
3
4
5
math.randomseed(os.clock())
function randomplayer()
	local t = player(0,"table")
	return t[math.rand(1,#t)]
end

old Re: Pick a player at random

DanielG
User Off Offline

Quote
Two questions, how would I make it only choose non-spec players, and which in the variable that is the number being chosen?

old Re: Pick a player at random

Mechanolith
User Off Offline

Quote
Yeah, i would like to know that too...
Picking random players is good for things like changing the player into a nemesis (i will need on a future script, im certain of it).

old Re: Pick a player at random

EngiN33R
Moderator Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
math.randomseed(os.clock())
function randomplayer()
     local t = player(0,"table")
     local id=t[math.rand(1,#t)]
     if (player(id,"team")~=0) then
          return id
     else
          randomplayer()
     end
end
This should work (I think). Nasty self-calling.

old Re: Pick a player at random

Starkkz
Moderator Off Offline

Quote
TKD, i used for some game modes that thing, it's not 100% working. If the player count is 0, then you can't select someone random into 1 and 0, so:

1
2
3
4
5
6
7
function randomplayer()
	local t = player(0,"table")
	if #t > 0 then
		return t[math.rand(1,#t)]
	end
	return nil
end

And returns nil if nobody is in the game.
edited 1×, last 23.05.11 12:35:38 am

old Re: Pick a player at random

archmage
User Off Offline

Quote
user DanielG has written
how would I make it only choose non-spec players


user Starkkz has written
If the player count is 0, then you can't select someone random into 1 and 0


1
2
3
4
function randomplayer()
	local t = player(0, "tableliving")
	return #t > 0 and t[math.random(1,#t)] or 0
end

old Re: Pick a player at random

archmage
User Off Offline

Quote
You need to store the return value of randomplayer.
1
2
local rand_id = randomplayer()
parse("equip "..rand_id.." 45")

old Re: Pick a player at random

RAVENOUS
BANNED Off Offline

Quote
user ohaz has written
correction:
1
2
3
4
5
math.randomseed(os.clock())
function randomplayer()
	local t = player(0,"table")
	return t[math.rand(1,#t)]
end


os.clock() as randomseed initiator seems not to work well for me. I got the same person with second hook like 7 times in a row. Any other good initiator?

old Re: Pick a player at random

Hador
User Off Offline

Quote
user archmage has written
You should use os.time() not os.clock()


IT's better to use os.clock().

You can use os.time() too, but that would be senseless unless you use it at exactly the same time very day.

Which ID did the player it picked have?

old Re: Pick a player at random

DC
Admin Off Offline

Quote
user RAVENOUS has written
os.clock() as randomseed initiator seems not to work well for me. I got the same person with second hook like 7 times in a row. Any other good initiator?

you can't really avoid that by using another seed. things like that can always happen - because it's (pseudo) random. you might also get a 6 X times in a row when you throw a dice often enough.
what you could do is saving the last X results of that function in variable and let it select another player if it chooses one of the x saved results again.

old Re: Pick a player at random

Lee
Moderator Off Offline

Quote
user Hador has written
user archmage has written
You should use os.time() not os.clock()


IT's better to use os.clock().

You can use os.time() too, but that would be senseless unless you use it at exactly the same time very day.

Which ID did the player it picked have?


No, os.clock returns the number of milliseconds that the script has ran (If you started the script 67 milliseconds ago, os.clock() will return the number 67) whereas os.time returns the number of seconds passed since 1970. Under ideal conditions, os.clock should theoretically return a very limited range of seeds whereas os.time will return better seeds. However, it may be advisable to mask all but the lowest 32 bytes of os.time.

old Re: Pick a player at random

FlooD
GAME BANNED Off Offline

Quote
i really dont think anyone will notice how random it is as long as you dont restart the server every minute...

old Re: Pick a player at random

EngiN33R
Moderator Off Offline

Quote
I like how the simplest question thread has become a thread with a bunch of pro scripters discussing what is better to use as the seed for math.random - os.clock or os.time. Seriously, come on.
To the start Previous 1 2 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview