Re: Newbie question--next_in_room

From: Erwin S. Andreasen (erwin@PIP.DKNET.DK)
Date: 09/20/97


On Fri, 19 Sep 1997, Gary Barnett wrote:

> > > > it goes through the occupants on a room one by one so that I can make a
> > > > spell that hits a character at random.  Anyone know of a place in the

> Well, you have two choices.

> 2) Harder - Run through the loop once and count how many targets you
>     have. Then pick one at random, then run through the loop again until
>     you reach the one you want. You could also create an array or linked
>     list to hold the people in the room so you could pick directly, but that
>     approach isn't indicated here, imho.

You can do this in one loop using something like this:

target = NULL;
count = 0;
for (ch = people in room; ch; ch = next after ch in room)
        if (other restrictions here, like ch must be visible or whatever)
                if (number_range(1, ++count) == 1)
                        target = ch;

If a target was found, 'target' will be set to to them. This loop gives
equal chance to picking any target in room. For example, with 1 target,
the random check will be number_range(1,1) == 1, i.e. 100%. with 2
targets, there will be 100% chance that the first one will be picked - but
the loop will continue, with a 50% chance of picking the second character
(effectively making the chance of picking the first character 50% too).

Above code is pseudo code since I do not use Circle, and assumes that
number_range() is not a macro. Hm, in case it's named differently in
Circle, number_range(a,b) will return a random number from a to b, both
number inclusive :)

 =============================================================================
Erwin Andreasen   Herlev, Denmark <erwin@pip.dknet.dk>  UNIX System Programmer
<URL:http://pip.dknet.dk/~erwin/>         <*>           (not speaking for) DDE
 =============================================================================


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



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