[comp.lang.pascal] THINK Pascal on the Mac

plague@milton.u.washington.edu (Jack Brown) (02/26/91)

I've got a problem.  I need to be able to generate random integers between
1 and 100. These are for indexing arrays.  Anyway, the problem is that when
I call the funciton 'random', I get integers between +-32767.  It is simple
to scale these to provide numbers between 1 and 100, but I can't seem to get
integers out of them...

Can anybody out there help?


Thanks,
Jack Brown
aka plague@milton.u.washington.edu

christer@cs.umu.se (Christer Ericson) (02/27/91)

In article <1991Feb26.161857.17056@odin.diku.dk> dat0@diku.dk (Dat-0 undervisningsassistent) writes:
>hofer@urz.unibas.ch (Remo Hofer) writes:
>
>>In article <17150@milton.u.washington.edu>, plague@milton.u.washington.edu (Jack Brown) writes:
>>> I need to be able to generate random integers between
>>> 1 and 100. These are for indexing arrays.  Anyway, the problem is that when
>>> I call the funciton 'random', I get integers between +-32767.  It is simple
>>> to scale these to provide numbers between 1 and 100, but I can't seem to get
>>> integers out of them...
>
>>try:
>>  x := abs(random) MOD 100 + 1;
>
>Which won't work since abs returns a real when given a real. Also it
>violates the propability-distribution.

But it will, since random returns an integer. This was on a Mac, remember?
Random numbers was discussed on comp.sys.mac.programmer some while ago and
the better bits can be found in the 'Usenet Macintosh Programmer's Guide'
(UMPG), available at the sumex archives.

> [ stuff deleted ]
>
>Kristian Damm Jensen (dat0@diku.dk)
>Institute of datalogi, University of Copenhagen (DIKU)
>Universitetsparken 1, DK-2100 Copenhagen \, Denmark


| Christer Ericson                            Internet: christer@cs.umu.se |
| Department of Computer Science, University of Umea, S-90187 UMEA, Sweden |

clear@cavebbs.gen.nz (Charlie Lear) (02/28/91)

In article <17150@milton.u.washington.edu> plague@milton.u.washington.edu (Jack Brown) writes:
>I've got a problem.  I need to be able to generate random integers between
>1 and 100. These are for indexing arrays.  Anyway, the problem is that when
>I call the funciton 'random', I get integers between +-32767.  It is simple
>to scale these to provide numbers between 1 and 100, but I can't seem to get
>integers out of them...

Try putting the range after the random function, for example
 random(100)

Works on most compilers.
-- 
--------------------------------------------------------------------------
Charlie "The Bear" Lear | clear@cavebbs.gen.nz | Kawasaki Z750GT  DoD#0221
The Cave MegaBBS  +64 4 642260  V32 | PO Box 2009, Wellington, New Zealand
--------------------------------------------------------------------------