comp.lang.ada
 help / color / mirror / Atom feed
From: johnherro@aol.com (John Herro)
Subject: Re: Random Number problems
Date: 1997/10/01
Date: 1997-10-01T00:00:00+00:00	[thread overview]
Message-ID: <19971001143301.KAA07525@ladder01.news.aol.com> (raw)


David Muhammad <guru@activist.com> writes:
> I have written the Random Number Generator
> function but how do I tell the compiler to
> select a number between 1and 52?

It depends on what your function Rand_Num returns.  Suppose Rand_Num
returns a Float between 0.0 and 1.0.  Then the following should work in
both Ada 95 and Ada 83.  If you have "subtype Pseudorand is Integer range 1
.. 52;" and you declare "P : Pseudorand;", you could write

P := Pseudorand(Rand_Num*52.0 + 0.5);

The expression within the parentheses is a Float.  When Ada converts a
Float to an integer type, it rounds.  That's why I added 0.5 and not 1.0. 
If Rand_Num is just above 0.0, P will be 1, and if Rand_Num is just below
1.0, P will be 52.

With some Ada compilers, it's not certain which way it will round if the
fractional part of the Float is exactly 0.5.  So if your Rand_Num function
might return exactly 0.0 or 1.0, you may have to add code to check that the
result of rounding is between 1 and 52 before storing in P.  (E.g., if the
result is 0, force it to 1 before storing, and force a result of 53 to 52.)
 I hope this helps.

- John Herro
Download the shareware Ada Tutor program at
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor




             reply	other threads:[~1997-10-01  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-10-01  0:00 John Herro [this message]
  -- strict thread matches above, loose matches on Subject: below --
1997-09-30  0:00 Stream_Access value valid across Close/Open? Dale Stanbrough
1997-09-30  0:00 ` Random Number problems David Muhammad
1997-10-01  0:00   ` Geert Bosch
1997-10-01  0:00   ` Martin C. Carlisle
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox