From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: How to shuffle/jumble letters of a given word? Date: Sun, 28 Jun 2015 22:07:49 +0100 Organization: A noiseless patient Spider Message-ID: References: <24d183ce-77b6-4fe1-a95e-0ff5ef72c7bd@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="c762050aff3a30866fa7e79999b14776"; logging-data="15607"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19cvfGHLvdGRUFb8RlswcxkLmdboWdr89k=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (darwin) Cancel-Lock: sha1:V8LcVOexZijrjH0la2Q7cqUoS/E= sha1:rGI2XlcewWZ+i9w3rUbatQLyZ6w= Xref: news.eternal-september.org comp.lang.ada:26517 Date: 2015-06-28T22:07:49+01:00 List-Id: Trish Cayetano writes: > 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 You want to take the letters in a random order, so you'll find Ada.Numerics.Float_Random[1] useful. You can't just take the random'th character of the original string, because you mustn't take the same character twice. One (possibly heavyweight) approach would be to create a new type containing a float and a character, type Item is record Key : Float; Ch : Character; end record; and a type for an array of Items, rather like the String you want to jumble, type Elements is array (Positive range <>) of Item; and then instantiate Ada.Containers.Generic_Array_Sort[2] for Elements. You'll need a function "<" to compare two Items on the basis of their Keys, e.g. function "<" (L, R : Item) return Boolean is (L.Key < R.Key); Then, given a String, create an Elements array with each Key a new random number and each Ch the corresponding character in the String; sort it (which, since the Keys were random, is effectively a shuffle); and return a String formed of the Chs in their new order. [1] http://www.ada-auth.org/standards/12rm/html/RM-A-5-2.html [2] http://www.ada-auth.org/standards/12rm/html/RM-A-18-26.html#p2