[comp.sys.mac.programmer] Random

umcarls9@ccu.umanitoba.ca (11/26/89)

I have the following in LightspeedC 3.02
 
long int ltime;
     int number;

     GetDateTime(&ltime);
     randSeed=ltime;
     number=Random();

Everything compiles fine, but when I run, the system comes up with a
bomb box ID 12 when it trys to do the number=Random();.   I've even
tried it with just that line, and left randSeed set to its default
and it still bombs.   
What am I doing wrong???
 
Is there a good book anyone can recommend on learning C?
 
Thanks...

 

CXT105@PSUVM.BITNET (Christopher Tate) (11/28/89)

In article <1989Nov26.011221.18046@ccu.umanitoba.ca>, umcarls9@ccu.umanitoba.ca
says:

>I have the following in LightspeedC 3.02
>
>long int ltime;
>     int number;
>
>     GetDateTime(&ltime);
>     randSeed=ltime;
>     number=Random();
>
>Everything compiles fine, but when I run, the system comes up with a
>bomb box ID 12 when it trys to do the number=Random();.   I've even
>tried it with just that line, and left randSeed set to its default
>and it still bombs.
>What am I doing wrong???

Before you can call Random(), you _must_ call InitGraf(&thePort).  This
sets up the random number generator.

-------
Christopher Tate                    |  "And as I watch the drops of rain
                                    |   Weave their weary paths and die,
cxt105@psuvm.psu.edu                |   I know that I am like the rain;
{...}!psuvax1!psuvm.bitnet!cxt105   |   There but for the grace of you go I."
cxt105@psuvm.bitnet                 |          -- Simon & Garfunkle

CXT105@psuvm.psu.edu (Christopher Tate) (02/19/91)

In article <1991Feb18.221718.9087@portia.Stanford.EDU>,
ralphm@portia.Stanford.EDU (Ralph Melton) inquires:

>One last question:  Is it inappropriate for me to call Random from within
>an After Dark module?  I realize that, since I don't have globals, if I
>call Random, I am stepping on the RandSeed global of someone else's
>A5 world.  But RandSeed is supposed to contain a pseudorandom number.
>If I replace it with a different pseudorandom number, will I give any
>program problems?

You might want to put in your own random number seed, and then replace
the original contents of randSeed after calling Random().  If you just
trash the randSeed, it's certainly conceivable that you will interfere
with someone who's running statistical simulations of some sort, and
is relying on having a predictable sequence of numbers generated by
Random().

-------
Christopher Tate                          |
                                          | Are they purple, and eat people,
Uucp:  {...}!psuvax1!psuvm.bitnet!cxt105  | or do they just eat purple people?
Internet:  cxt105@psuvm.psu.edu           |

beard@ux5.lbl.gov (Patrick C Beard) (02/20/91)

In article <91050.080755CXT105@psuvm.psu.edu> CXT105@psuvm.psu.edu (Christopher Tate) writes:
#In article <1991Feb18.221718.9087@portia.Stanford.EDU>,
#ralphm@portia.Stanford.EDU (Ralph Melton) inquires:
#
#>One last question:  Is it inappropriate for me to call Random from within
#>an After Dark module?  I realize that, since I don't have globals, if I
#>call Random, I am stepping on the RandSeed global of someone else's
#>A5 world.  But RandSeed is supposed to contain a pseudorandom number.
#>If I replace it with a different pseudorandom number, will I give any
#>program problems?
#
#You might want to put in your own random number seed, and then replace
#the original contents of randSeed after calling Random().  If you just
#trash the randSeed, it's certainly conceivable that you will interfere
#with someone who's running statistical simulations of some sort, and
#is relying on having a predictable sequence of numbers generated by
#Random().

If my memory serves me, After Dark does that for you.  It IS safe to call
Random() from After Dark modules (we have many that do).

--
------------------------------------------------------------------
|  Patrick C. Beard, Software Engineer, Berkeley Systems, Inc.   |
|                    "Heroes of technology."                     |
|    beard@lbl.gov, d0346@applelink.apple.com (ATTN: Patrick)    |

egw.weakm@p3.lanl.gov (Eric Wasserman) (02/20/91)

In article <10063@dog.ee.lbl.gov> beard@ux5.lbl.gov (Patrick C Beard) 
writes:
One last question:  Is it inappropriate for me to call Random from within
> an After Dark module?  I realize that, since I don't have globals, if I
> call Random, I am stepping on the RandSeed global of someone else's
> A5 world.  But RandSeed is supposed to contain a pseudorandom number.
> If I replace it with a different pseudorandom number, will I give any
> program problems?

Why don't you just use your own random number routine?  A standard linear-
congruential generator is essentially one line of code and is every bit
as good as the built-in Random(). At the same time, it is completely 
portable
and won't screw up the global RandSeed.  See the UMPG chapt. 1, or Knuth's
"Seminumerical Algorithms", for examples.

Eric

Eric
egw.weakm@p3.lanl.gov