comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <onewingedshark@gmail.com>
Subject: Re: Question: re Image Files.
Date: Fri, 31 Aug 2012 15:09:25 -0700 (PDT)
Date: 2012-08-31T15:09:25-07:00	[thread overview]
Message-ID: <aac80710-ff65-41b8-a4e5-c87f94d766bb@googlegroups.com> (raw)
In-Reply-To: <3a9f8943-34af-43b2-ab27-aad438fa0974@googlegroups.com>

Here's one for you then. It builds a shuffled array over the given type, which can be used to index into something like a deck of cards or somesuch.

Generic
    Type Element is (<>);
    Low  : Natural:= Element'Pos(Element'First);
    High : Natural:= Element'Pos(Element'Last);
Package Random is
    SubType Element_Range is Integer Range Low..High;
    Function Incrementor Return Element;
    Type Element_Array is Array(Element_Range) of Element;

    Values : Element_Array;
    Procedure Print;
End Random;

--------------------------

With
	Ada.Text_IO,
	Ada.Numerics.Discrete_Random;

Use
	Ada.Text_IO;

Package Body Random is
    Count : Element := Element'First;

    Function To_Integer( Input : in Element ) Return Element_Range is
    begin
	Return Low;
    end;
    
    
    Function Incrementor Return Element is
    begin
	Return Result : Element:= Count do
	    Null;
	    Count:= Element'Succ( Result );
	Exception
	    When Constraint_Error => Count:= Element'First;
	End Return;
    end Incrementor;

    Procedure Swap( Index_1, Index_2 : In Integer ) is
	Temp : Constant Element:= Values( Integer(Index_1) );
    begin
	Values( Integer(Index_1) ):= Values( Integer(Index_2) );
	Values( Integer(Index_2) ):= Temp;
    end Swap;

    Procedure Print is
    begin
	Put_Line( "Length: " & Values'Length'Img );
	Put( "(" );
	For Index in Values'First..Integer'Pred(Values'Last) loop
	    Put( Values(Index)'Img & ',' );
	end loop;
	Put( Values(Values'Last)'Img );
	Put_Line( ")" );
    end Print;


Begin
    null;
    Shuffle:
    Declare
	Package Random_Element is New
	  Ada.Numerics.Discrete_Random( Element_Range );
	Number : Random_Element.Generator;
	Use Random_Element;
	Function Random( Gen : Generator ) Return Element_Range
		 Renames Random_Element.Random;
    Begin
	Reset( Number );
	Values:= Element_Array'( Others => Incrementor );
	       For Index in Element_Array'Range loop
	          Swap( Index, Random(Number) );
	       end loop;
    End Shuffle;
End Random;



  reply	other threads:[~2012-08-31 22:09 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-30 22:33 Question: re Image Files Austin Obyrne
2012-08-30 22:55 ` Adam Beneschan
2012-08-31  1:35 ` tmoran
2012-08-31  7:16 ` Dmitry A. Kazakov
2012-08-31  7:18 ` Mark Murray
2012-08-31  8:58   ` Austin Obyrne
2012-08-31  9:25     ` Ludovic Brenta
2012-08-31  9:41       ` Dmitry A. Kazakov
2012-08-31  9:14   ` Austin Obyrne
2012-08-31 17:09     ` Mark Murray
2012-08-31 17:36       ` Austin Obyrne
2012-08-31 22:09         ` Shark8 [this message]
2012-09-04 10:05         ` Antti Louko
2012-08-31 21:12   ` gautier_niouzes
2012-09-01  6:58     ` Austin Obyrne
2012-09-03  6:02   ` Anatoly Chernyshev
2012-09-03  7:27     ` Mark Murray
2012-08-31  8:41 ` Brian Drummond
replies disabled

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