comp.lang.ada
 help / color / mirror / Atom feed
* Random Number problems
  1997-09-30  0:00 Stream_Access value valid across Close/Open? Dale Stanbrough
@ 1997-09-30  0:00 ` David Muhammad
  1997-10-01  0:00   ` Martin C. Carlisle
  1997-10-01  0:00   ` Geert Bosch
  0 siblings, 2 replies; 4+ messages in thread
From: David Muhammad @ 1997-09-30  0:00 UTC (permalink / raw)



>I am trying to write a function that will
 use a Random Number Generator and then return a random value between
1 and 52. I have written the Random  Number Generator function but how
do I tell the compiler to select a number between 1 and  52.

a) declare a subtype
	subtype pseudorand is range 1..52
b) Call my randomnumber from function Rand_Num 
c) NOw somehow convert some "Number" to a range between 1 and 52.
I am stuck ??




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Random Number problems
@ 1997-10-01  0:00 John Herro
  0 siblings, 0 replies; 4+ messages in thread
From: John Herro @ 1997-10-01  0:00 UTC (permalink / 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




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Random Number problems
  1997-09-30  0:00 ` Random Number problems David Muhammad
  1997-10-01  0:00   ` Martin C. Carlisle
@ 1997-10-01  0:00   ` Geert Bosch
  1 sibling, 0 replies; 4+ messages in thread
From: Geert Bosch @ 1997-10-01  0:00 UTC (permalink / raw)



David Muhammad <guru@activist.com> wrote:
   >I am trying to write a function that will
    use a Random Number Generator and then return a random value between
   1 and 52. I have written the Random  Number Generator function but how
   do I tell the compiler to select a number between 1 and  52.

   a) declare a subtype
   	subtype pseudorand is range 1..52
   b) Call my randomnumber from function Rand_Num 
   c) NOw somehow convert some "Number" to a range between 1 and 52.
   I am stuck ??

A quick quote from the nice RM:

  50    (16) A given implementation of the Random function in
	Numerics.Float_Random may or may not be capable of delivering
	the values 0.0 or 1.0. Portable applications should assume
	that these values, or values sufficiently close to them to
	behave indistinguishably from them, can occur.  If a sequence
	of random integers from some fixed range is needed, the
	application should use the Random function in an appropriate
	instantiation of Numerics.Discrete_Random, rather than
	transforming the result of the Random function in
	Numerics.Float_Random.  However, some applications with
	unusual requirements, such as for a sequence of random
	integers each drawn from a different range, will find it
	more convenient to transform the result of the floating
	point Random function.  For M>=1, the expression

        51       Integer(Float(M) * Random(G)) mod M
        

  52    transforms the result of Random(G) to an integer uniformly
	distributed over the range 0 .. M-1; it is valid even if
	Random delivers 0.0 or 1.0.  Each value of the result range
	is possible, provided that M is not too large.  Exponentially
	distributed (floating point) random numbers with mean and
	standard deviation 1.0 can be obtained by the transformation

        53       -Log(Random(G) + Float'Model_Small))
        

  54    where Log comes from Numerics.Elementary_Functions (see
	A.5.1); in this expression, the addition of Float'Model_Small
	avoids the exception that would be raised were Log to be
	given the value zero, without affecting the result (in most
	implementations) when Random returns a nonzero value.





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Random Number problems
  1997-09-30  0:00 ` Random Number problems David Muhammad
@ 1997-10-01  0:00   ` Martin C. Carlisle
  1997-10-01  0:00   ` Geert Bosch
  1 sibling, 0 replies; 4+ messages in thread
From: Martin C. Carlisle @ 1997-10-01  0:00 UTC (permalink / raw)



In article <3431B598.41C6@activist.com>,
David Muhammad  <guru@activist.com> wrote:
>>I am trying to write a function that will
> use a Random Number Generator and then return a random value between
>1 and 52. I have written the Random  Number Generator function but how
>do I tell the compiler to select a number between 1 and  52.
>
>a) declare a subtype
>	subtype pseudorand is range 1..52
>b) Call my randomnumber from function Rand_Num 
>c) NOw somehow convert some "Number" to a range between 1 and 52.
>I am stuck ??

Most people would instead use the very nice built-in random number
generator.

E.g.  

package my_random is new Ada.Numerics.Discrete_Random(pseudorand);
g : my_random.generator;
x : pseudorand;

begin

my_random.reset(g);
x := my_random.random(g);

For more info, see LRM A.5.2, Barnes p. 54, Feldman pp 320-322,
Cohen p. 93

If you insist on using your own, you want to use mod.

--Martin

-- 
Martin C. Carlisle, Computer Science, US Air Force Academy
mcc@cs.usafa.af.mil, http://www.usafa.af.mil/dfcs/bios/carlisle.html
DISCLAIMER:  This content in no way reflects the opinions, standard or 
policy of the US Air Force Academy or the United States Government.




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1997-10-01  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-10-01  0:00 Random Number problems John Herro
  -- 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   ` Martin C. Carlisle
1997-10-01  0:00   ` Geert Bosch

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