* Stream_Access value valid across Close/Open?
@ 1997-09-30 0:00 Dale Stanbrough
1997-09-30 0:00 ` Random Number problems David Muhammad
0 siblings, 1 reply; 4+ messages in thread
From: Dale Stanbrough @ 1997-09-30 0:00 UTC (permalink / raw)
Is an Ada.Streams.Stream_IO.Stream_Access value valid across calls
to Close/Open of the associated File variable? The LRM is silent
on this issue, but I would guess not. Gnat 3.09 lets me do it,
but I suspect this falls into the realm of "erroneous behaviour".
Dale
^ permalink raw reply [flat|nested] 4+ messages in thread
* 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-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
* 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
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-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