swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Fwd: Re: Random number generators. advice?


From: Sven Thommesen
Subject: Fwd: Re: Random number generators. advice?
Date: Thu, 22 Feb 2001 08:33:12 -0600

--- Begin Message --- Subject: Fwd: Re: Random number generators. advice? Date: Thu, 22 Feb 2001 08:19:51 -0600
Paul,

also be aware that the mapping from objC unsigneds to java signed ints, (which 
happens automatically in the Swarm interface code) while linear, is not 
monotonic. It goes like this:

ObjC         Java
―------------------
0              -> 0
2^31-1    -> 2^31-1
―------------------
2^31       -> -2^31
2^32-1    -> -1
―-------------------

Notice the flip from 2^31-1 t- -2^31 on the Java side. This means that code on 
the ObjC side of the sort "if (var1 < var2) {}" may give different results in 
Java than it does in ObjC.

Oh, and the problem happens in reverse as well ― for example, generators take 
unsigned ints as seeds in ObjC and to explore the full range of possible seeds 
you'd need to use negative numbers as well in Java. Inverting the above mapping 
is left as an entertaining exercise for the reader ...

Sven
--- Begin Message --- Subject: Re: Random number generators. advice? Date: Wed, 21 Feb 2001 12:57:43 -0600

I should make the following additional comment since you are trying to do the same thing in ObjC and in Java:

As you know, in ObjC the generators provide random variates in the form of doubles [0.0, 1.0) or unsigned integers [0, max-1]. For 32-bit generators, "max" may be as high as 2^32-1. For 31-bit generators, "max" is less than 2^31.

Unfortunately, Sun designers in their wisdom decided to equip java with *signed* integers only, in the range [-2^31, +2^31-1]. Consider the fact that the underlying random generators in Swarm are written in ObjC. So when a Java program calls a 32-bit generator (such as the default one), variates that exceed 2^31 will map to a negative number in Java.

(31-bit generators are not affected by this issue.)

If your simulation expects a positive random integer, you might want to throw away all negative variates in Java. (And if you want the ObjC and Java sims to run the same, you'd want to throw away variates larger than 2^31 in ObjC as well.) Doing this will of course have unknown effects on the quality of the random streams you end up using.

Or, use only the floating point variates from the generators, and map those to integers as needed.

Sven



At 10:40 AM 2/21/2001 -0600, you wrote:
I'm trying to make two "similar" models, one in obj-c and one in java,
generate the exact same results.  In order to do that, I need the exact
same random numbers at the start, and I'm having trouble because one
program makes draws on the random generator more than the other, so the
numbers for some variables don't match up.

As a result, I need to create three or four different generator objects
which can be called at just the right time so that the numbers will be
identical in the two simulations.  At random, I chose to add the
generator C3MWCgen and a couple others.  On the objc side that's OK, but
there is no Java Swarm interface for that one.

So then I thought I might as well just ask the experts which generators
to use, if one needs separate generators to keep random numbers matched
up.

Advise me please.
--
Paul E. Johnson                       email: address@hidden
Dept. of Political Science            http://lark.cc.ukans.edu/~pauljohn
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66045                FAX: (785) 864-5700

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.


                 ==================================
  Swarm-Support is for discussion of the technical details of the day
  to day usage of Swarm.  For list administration needs (esp.
  [un]subscribing), please send a message to <address@hidden>
  with "help" in the body of the message.


--- End Message ---

--- End Message ---

reply via email to

[Prev in Thread] Current Thread [Next in Thread]