Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Random number generation

49 views
Skip to first unread message

tonyg

unread,
Jul 13, 2010, 8:45:47 AM7/13/10
to
I want to generate a random integer and a random floating point number
between 10 and 30 . I've been looking at previous posts and finding it
a little confusing with many packages, has anyone an already done
example in ada 2005 they can post up?

Jacob Sparre Andersen

unread,
Jul 13, 2010, 8:50:32 AM7/13/10
to
tonyg <tonyt...@googlemail.com> writes:

There is an example in section A.5.2 in the reference manual.

Jacob
--
There only exist 10 kinds of people: Those who know binary
numbers and those who don't know binary numbers.

Dmitry A. Kazakov

unread,
Jul 13, 2010, 8:58:40 AM7/13/10
to

An example of Gaussian distribution:

http://rosettacode.org/wiki/Random_numbers#Ada

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

Thomas Løcke

unread,
Jul 13, 2010, 9:17:56 AM7/13/10
to


Hey tonyq,

I wrote this little tutorial a while ago:

http://wiki.ada-dk.org/index.php/The_Dice_Roller_Program

As the name suggests, the tutorial centers around a program that rolls
dice, and as such it needs to generate some random numbers. The part the
does the random stuff can be found here:

http://wiki.ada-dk.org/index.php/The_Dice_Roller_Program#dice-random.ads_and_dice-random.adb

It's not a very complicated routine, so you should be able to tweak it
to match your needs. :o)

--
Regards,
Thomas Lųcke

Email: tl at ada-dk.org
Web: http:ada-dk.org
IRC nick: ThomasLocke

Jeffrey R. Carter

unread,
Jul 13, 2010, 12:07:57 PM7/13/10
to

The random integer is pretty easy to do with an appropriate [sub]type and
Ada.Numerics.Discrete_Random. The random floating-point is a bit more difficult,
and there's always the question of whether 30.0 must be a possible value. You
can have a look at Random_Range in PragmARC.Universal_Random:

http://pragmada.x10hosting.com/pragmarc.htm

which excludes the maximum value from the returned values.

--
Jeff Carter
"When Roman engineers built a bridge, they had to stand under it
while the first legion marched across. If programmers today
worked under similar ground rules, they might well find
themselves getting much more interested in Ada!"
Robert Dewar
62

John B. Matthews

unread,
Jul 13, 2010, 4:33:39 PM7/13/10
to
In article <i1i3cc$8ro$1...@tornado.tornevall.net>,

"Jeffrey R. Carter" <spam.jrc...@spam.acm.org> wrote:

> On 07/13/2010 05:45 AM, tonyg wrote:
> > I want to generate a random integer and a random floating point
> > number between 10 and 30 . I've been looking at previous posts and
> > finding it a little confusing with many packages, has anyone an
> > already done example in ada 2005 they can post up?

Here's an Ada 95 example that uses Ada.Numerics.Discrete_Random to
simulate repeated plays of a simple card game:

<http://home.roadrunner.com/~jbmatthews/war.html>

The shuffling algorithm needs work; this might be an alternative:

<http://en.wikipedia.org/wiki/Fisher–Yates_shuffle>

> The random integer is pretty easy to do with an appropriate [sub]type
> and Ada.Numerics.Discrete_Random. The random floating-point is a bit
> more difficult, and there's always the question of whether 30.0 must
> be a possible value. You can have a look at Random_Range in
> PragmARC.Universal_Random:
>
> http://pragmada.x10hosting.com/pragmarc.htm

As an aside, Mine Detector V6.0 builds on Mac OS X 10.5.8 with GtkAda
2.14.1 and GNAT 4.3.4. Wait, I took a picture:

<http://i26.tinypic.com/23qyxxi.png>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Jeffrey R. Carter

unread,
Jul 13, 2010, 7:02:49 PM7/13/10
to
On 07/13/2010 01:33 PM, John B. Matthews wrote:
>
> The shuffling algorithm needs work; this might be an alternative:
>
> <http://en.wikipedia.org/wiki/Fisher–Yates_shuffle>

The problem with this (for Ada) is the need to instantiate Discrete_Random for a
different range each time (although you could use
PragmARC.Universal_Random.Random_Int). If it's for a game with a human, then the
biased version of using the full range each time is good enough

> As an aside, Mine Detector V6.0 builds on Mac OS X 10.5.8 with GtkAda
> 2.14.1 and GNAT 4.3.4. Wait, I took a picture:
>
> <http://i26.tinypic.com/23qyxxi.png>

Cool. I wouldn't check any boxes with only 100 mines.

John B. Matthews

unread,
Jul 14, 2010, 12:42:39 AM7/14/10
to
In article <i1irmb$vfe$1...@tornado.tornevall.net>,

"Jeffrey R. Carter" <spam.jrc...@spam.acm.org> wrote:

> On 07/13/2010 01:33 PM, John B. Matthews wrote:
> >
> > The shuffling algorithm needs work; this might be an alternative:
> >
> > <http://en.wikipedia.org/wiki/Fisher–Yates_shuffle>
>
> The problem with this (for Ada) is the need to instantiate
> Discrete_Random for a different range each time (although you could
> use PragmARC.Universal_Random.Random_Int).

Thank you for suggesting this; I see the problem, now. Section
A.5.2(50), Note 16, mentions the very problem, suggesting Float_Random
as an alternative:

<http://www.adaic.com/standards/05rm/html/RM-A-5-2.html>

I also found this implementation using Discrete_Random:

<http://rosettacode.org/wiki/Knuth_shuffle#Ada>

> If it's for a game with a human, then the biased version of using the

> full range each time is good enough.

No, it's a simulation; I don't get off that easy.



> > As an aside, Mine Detector V6.0 builds on Mac OS X 10.5.8 with GtkAda
> > 2.14.1 and GNAT 4.3.4. Wait, I took a picture:
> >
> > <http://i26.tinypic.com/23qyxxi.png>
>
> Cool. I wouldn't check any boxes with only 100 mines.

They were smiling for the camera! :-)

tonyg

unread,
Jul 15, 2010, 3:01:06 PM7/15/10
to
On 14 July, 05:42, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article <i1irmb$vf...@tornado.tornevall.net>,

Thanks for the help guys

0 new messages