From: MM <mrvmurray@gmail.com>
Subject: Re: How to shuffle/jumble letters of a given word?
Date: Mon, 29 Jun 2015 14:45:13 -0700 (PDT)
Date: 2015-06-29T14:45:13-07:00 [thread overview]
Message-ID: <3a73a637-e02a-464f-ae7f-dfb04dac70ee@googlegroups.com> (raw)
In-Reply-To: <24d183ce-77b6-4fe1-a95e-0ff5ef72c7bd@googlegroups.com>
On Sunday, 28 June 2015 15:06:29 UTC+1, Trish Cayetano wrote:
> I am creating a "text twist game" where you guess the words in a jumbled manner. How do I set the code to shuffle the letters?
>
> Example: word to be guessed is HELLO
>
> What should be displayed is: LOLEH or LELHO
>
> Thanks in advance!
Here is a rough-and-ready Fisher-Yates shuffle demonstration. This one shuffles whatever line it receives on STDIN (only one line).
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Discrete_Random;
procedure Shuffle_String is
function ShuffleRand (N: in Positive) return Integer is
subtype Random_Range is Integer range 1 .. N;
package Random_Shuffle_Integer is new Ada.Numerics.Discrete_Random(Random_Range);
use Random_Shuffle_Integer;
Generator : Random_Shuffle_Integer.Generator;
Retval: Random_Range;
begin
Reset(Generator);
Retval := Random(Generator);
return Retval;
end;
function Shuffle (Item : String) return String is
Result : String (Item'Range);
Temp : Character;
J : Integer;
begin
Result := Item;
for I in reverse Result'first + 1 .. Result'last loop
J := ShuffleRand (I);
Temp := Result (I);
Result (I) := Result (J);
Result (J) := Temp;
end loop;
return Result;
end Shuffle;
begin
Put_Line (Shuffle (Get_Line));
end Shuffle_String;
next prev parent reply other threads:[~2015-06-29 21:45 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-06-28 14:06 How to shuffle/jumble letters of a given word? Trish Cayetano
2015-06-28 15:52 ` David Botton
2015-06-28 16:01 ` Anh Vo
2015-06-28 21:07 ` Simon Wright
2015-06-28 21:23 ` Jeffrey R. Carter
2015-06-29 8:05 ` Simon Wright
2015-06-29 8:13 ` Dmitry A. Kazakov
2015-06-29 18:04 ` Jeffrey R. Carter
2015-07-02 20:56 ` Randy Brukardt
2015-06-29 13:42 ` darkestkhan
2015-06-29 20:21 ` Austin Obyrne
2015-06-29 21:45 ` MM [this message]
2015-06-30 12:29 ` Austin Obyrne
2015-07-01 10:40 ` darkestkhan
2015-08-09 21:50 ` David Thompson
2015-08-10 15:20 ` darkestkhan
2015-07-11 15:29 ` Trish Cayetano
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox