[code] Picking a random target off a list (WAS: Mob Attacks)

From: Erwin S. Andreasen (erwin@pip.dknet.dk)
Date: 03/02/97


On Sun, 2 Mar 1997, Count Zero wrote:

> but I notice that it seems to be random with a HEAVY bent towards whomever
> the mob is currently attacking...  ie: in a test with two combatants the
> mob only hit the non-leader once out of 6 rounds... or so... and this seems

To pick a random member of a list, without knowing before how many members
there are, and giving equal chance to all of them, use something like
this:

count = 0;
target = NULL;

for (rch = <first list member>; rch; rch = rch->next_in_room)
	if (rch fulfills your conditions && number_range(0,count++) == 0)
		target = rch;

(number_range is called number in Circle I guess, it should return a
random number from a to b).

The first possible target that is encountered will have a 100% chance of
being picked. If there is another possible target, the chance of picking
that one will be 50% (now count is 1, so the random number will have 50%
of being 0). This also decreases the effective chance of the first
character being picked to 50%.

If there was at least one person that fullfills your conditions, 'target'
will be set to a random of these when the above loop finishes.

Your conditions in this case would be that FIGHTING(rch) == ch or
something like that.

==============================================================================
Erwin Andreasen  Viby J, Denmark   Computer Science Student at Aarhus Business
erwin@pip.dknet.dk        <URL:http://pip.dknet.dk/~pip1773/>          College
==============================================================================




+-----------------------------------------------------------+
| Ensure that you have read the CircleMUD Mailing List FAQ: |
|   http://cspo.queensu.ca/~fletcher/Circle/list_faq.html   |
|    Or send 'info circle' to majordomo@cspo.queensu.ca     |
+-----------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/18/00 PST