[MUD-Dev] Rooms, 3D arrays, etc.

clawrenc at cup.hp.com clawrenc at cup.hp.com
Thu Jun 5 11:28:42 CEST 1997


In <199706050211.VAA10068 at dfw-ix15.ix.netcom.com>, on 06/04/97 
   at 08:34 PM, "Jon A. Lambert" <jlsysinc at ix.netcom.com> said:

>Does multi-threading have any affects on seeds used in the random
>generator.  Does anyone know if this is truly thread safe?

Yes and no.  

Look at the random number algorithms in something like Numerical
Recipes.  Many random number algorithms work by getting the next value
by computing on that last value returned and possibly some earlier
ones than that.  sometimes this is also crossed with a timer value
either as an initial seed, or as an ongoing affair.

There are obvious MT problems here, the most glaring being the race
condition with on the last returned value for the next value
computation.

More interesting for the current fractal landscape discussion is
repeatability.  We don't really want random values.  We want values
that viewed as a set appear random, but can be re-computed from a
basic algorithm to get an identical set of values.  (That rules out
any of the timer absed seeds.)  Given an MT safe random number
function we have an extra constraint here in that given a certain
seed, rand() as called by the landscape code must ALWAYS generate the
same sequence of values, despite the fact that the fight code, spell
code etc amy also be calling rand() in other threads, quite possibly
with different seed values.

The standard approach is to make private rand() functions for each
feature.  This has the benefit of allowing the choice of algorithm for
each rand() function to be tailored to the task.  The problem is that
it does not solve the problem if your feature may be called from
multiple threads (eg multiple threads querying/generating your
landscape).  Next up is to use per-feature specific rand() functions
and then add some sort of rand() context to each thread (cf rand
object) which guarantees the sequence.  

--
J C Lawrence                           Internet: claw at null.net
(Contractor)                           Internet: coder at ibm.net
---------------(*)               Internet: clawrenc at cup.hp.com
...Honorary Member Clan McFUD -- Teamer's Avenging Monolith...




More information about the mud-dev-archive mailing list