Newsgroups: comp.ai.alife
Path: cantaloupe.srv.cs.cmu.edu!rochester!cornellcs!travelers.mail.cornell.edu!news.kei.com!news.mathworks.com!tank.news.pipex.net!pipex!sunsite.doc.ic.ac.uk!uknet!newsfeed.ed.ac.uk!ainews!peter
From: peter@aisb.ed.ac.uk (Peter Ross)
Subject: Re: Asynchronous/Random Updating of Cells
In-Reply-To: timothyh@aisb.ed.ac.uk's message of Thu, 12 Oct 1995 09:18:19 GMT
Message-ID: <PETER.95Oct12125149@balder.aisb.ed.ac.uk>
Sender: news@aisb.ed.ac.uk (Network News Administrator)
Organization: Dept. of AI, University of Edinburgh
References: <DGBwIK.Etp@aisb.ed.ac.uk>
Date: Thu, 12 Oct 1995 11:51:49 GMT
Lines: 44

In article <DGBwIK.Etp@aisb.ed.ac.uk> timothyh@aisb.ed.ac.uk (Timothy Hely) writes:

>  Could anyone tell me whether there is a "standard" technique/algorithm
>  for updating a grid of N cells randomly, so that each cell is updated
>  once only before moving on to the next time step.
>   (Preferably something that could be translated into a C function)
>   <..deleted..>
>   I imagine that random updating could be done as follows.
>   /* random.c */
>   (1) Create a linked list of size N where each link stores the number of
>  a cell and a pointer to the next link. Each link is allocated memory
>  individually.
>   (2) for(loop=0; loop<N ; loop++)
>      {
> (3) Choose a random number R between 0 and (N-loop);
>	 (4) Update the cell corresponding to link(R).
>	 (5) Free/delete this link so that now only (N-loop-1) links
>  remain. Update the pointer of the previous link.
>       }

It's customary to use some variant of Floyd's algorithm, eg to update
randomly-chosen M of a total of N cells:

    for(i=N-M; i < N; i++) {
       j = random() % (i+1);  /* random int in 0..i inclusive */
       if(updated[j]) update(i);
       else           update(j);
    }

This uses exactly M calls of random() and gets you a uniform
distribution over the N celss without duplication, depending
of course on how decent your random() is. 

No link-updating needed, although you still need to record
whether a cell got updated somehow.

A variant of this produces random sets - instead of update(i) insert i
just after j and instead of update(j) add j to front of set, and then
you get a random size-M-subset of 0..N-1 in random order.


Peter Ross
Dept of AI
University of Edinburgh
