* Lotto simulation 2
@ 2014-06-16 8:03 montgrimpulo
2014-06-16 12:21 ` Stefan.Lucks
2014-06-16 20:59 ` john
0 siblings, 2 replies; 3+ messages in thread
From: montgrimpulo @ 2014-06-16 8:03 UTC (permalink / raw)
Hi,
I want to thank all contributors to my first post on Lotto simulation for their valuable input.
As a conclusion I decided to use the containers.vectors package. Here is my solution. Please
feel free to comment on any aspect.
with Ada.Numerics.Discrete_Random, Ada.Text_IO, Ada.Containers.Vectors;
use Ada.Containers;
procedure Ziehung is
package IO renames Ada.Text_IO;
package ball_container is new Vectors (Positive, Positive);
balls, iball : ball_container.Vector;
tz, iz : Integer;
begin
balls.Set_Length (Length => 49);
iball.Set_Length (Length => 6);
for i in 1 .. 49 loop
balls.Replace_Element (Index => i, New_Item => i);
end loop;
tz := 49;
for i in 1 .. 6 loop
lottoblock : declare
subtype z is Positive range 1 .. tz;
package lb is new Ada.Numerics.Discrete_Random (z);
glb : lb.Generator;
begin
lb.Reset (glb);
iz := lb.Random (glb);
end lottoblock;
iball.Replace_Element
(Index => i, New_Item => balls.Element (Index => iz));
balls.Delete (Index => iz, Count => 1);
tz := tz - 1;
end loop;
IO.Put_Line ("Length_b:" & balls.Length'Img);
IO.Put_Line ("Length_ib:" & iball.Length'Img);
end Ziehung;
Length_b: 43
Length_ib: 6
[2014-06-16] process terminated successfully, elapsed time: 00.21s
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Lotto simulation 2
2014-06-16 8:03 Lotto simulation 2 montgrimpulo
@ 2014-06-16 12:21 ` Stefan.Lucks
2014-06-16 20:59 ` john
1 sibling, 0 replies; 3+ messages in thread
From: Stefan.Lucks @ 2014-06-16 12:21 UTC (permalink / raw)
[-- Attachment #1: Type: TEXT/PLAIN, Size: 1994 bytes --]
> I want to thank all contributors to my first post on Lotto simulation for their valuable input.
> As a conclusion I decided to use the containers.vectors package. Here is my solution. Please
> feel free to comment on any aspect.
Looks OK, in principle. Some of your identifiers may be too short to
understand what is going on (e.g., tz, iz), and, when communicating Ada
source code, you should lean to capitalize identifiers (Tz or TZ, but not
tz), ect ...
Also, it is a little surprising that you don't output your results.
However, my main criticism would be that your usage of Vectors is overkill
for the problem at hand. Here is a solution using a single array, instead
of two containers.
with Ada.Numerics.Discrete_Random, Ada.Text_IO;
procedure Ziehung2 is
subtype Index is Integer range 1 .. 49;
Ball: array(Index) of Index;
begin
-- intialize Ball
for I in Index loop
Ball(I) := I;
end loop;
-- draw six numbers and print them
for I in 0 .. 5 loop
-- invariant: Ball(1 .. Index'Last-I) hold the balls not yet drawn
-- Ball(Index'Last-I+1 .. Index'Last) hold the balls drawn so far
declare
subtype Z is Index range 1 .. Index'Last-I;
package RND is new Ada.Numerics.Discrete_Random(Z);
Gen: RND.Generator;
Drawn, Tmp: Index;
begin
RND.Reset(Gen);
Drawn := Rnd.Random(Gen); -- Drawn := random index
Ada.Text_IO.Put(Index'Image(Ball(Drawn))); -- print Ball(Drawn)
-- move Ball(Drawn) to the end of Ball(1 .. Index'Last-I)
-- more precisely, swap Ball(Index'Last-I) and Ball(Drawn)
Tmp := Ball(Index'Last-I);
Ball(Index'Last-I) := Ball(Drawn);
Ball(Drawn) := Tmp;
end;
end loop;
end Ziehung2
------ I love the taste of Cryptanalysis in the morning! ------
<http://www.uni-weimar.de/cms/medien/mediensicherheit/home.html>
--Stefan.Lucks (at) uni-weimar.de, Bauhaus-Universität Weimar, Germany--
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Lotto simulation 2
2014-06-16 8:03 Lotto simulation 2 montgrimpulo
2014-06-16 12:21 ` Stefan.Lucks
@ 2014-06-16 20:59 ` john
1 sibling, 0 replies; 3+ messages in thread
From: john @ 2014-06-16 20:59 UTC (permalink / raw)
Funny, my first Ada test was a lotto drawing program!
The archive at
http://bazaar.launchpad.net/~erich-snafu/lottery/release-2012-08-07/files
is still online, so you may check it out for comparison. I'm especially proud of my little bubblesort implementation ;-)
Haven't won anything yet, though :/
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2014-06-16 20:59 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-06-16 8:03 Lotto simulation 2 montgrimpulo
2014-06-16 12:21 ` Stefan.Lucks
2014-06-16 20:59 ` john
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox