-2

I want to populate 26 edit boxes with the letters A to Z. Already done. Now I want to click my Scramble button and rearrange all 26 letters randomly without any letter repeating. I've tried Delphi's Random, RandomRange, RanSeed, etc and various code snippets found via google but none of them work. I'm on XE8 if that helps.

Jerry Mallett
  • 75
  • 1
  • 6
  • This is a solved problem. Search for "Fisher-Yates shuffle". – r3mainer Mar 14 '21 at 21:29
  • Does this answer your question? [Efficient way to generate a random alphabet string?](https://stackoverflow.com/questions/14340974/efficient-way-to-generate-a-random-alphabet-string) – pjs Mar 15 '21 at 02:16

1 Answers1

0

I believe I found the answer. Most of the suggestions I've seen involve stringlists or listboxes which I didn't want to use. I wanted to keep it as simple as possible and this method, which I found on http://zarko-gajic.iz.hr/ appears to work well for integers so I modified if for my alphabet sort. I've run this multiple times and it seems fine to me but I'm just a Delphi Dabbler.....

// USING the simple Satolo cycle to unsort

Procedure SortIntegerList;
var a : Array of cardinal;
    i,j : integer;
      t : cardinal;
   Letter : String[1];
   Scrambled : String;
begin
  randomize;
  a := [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26];
  i := length(a);
  while i > 0 do
    begin
     dec(i);
     j := randomrange(Low(a),i);
     t := a[i];
     a[i] := a[j];
     a[j] := t;
     Letter := Chr(a[i]+64);
     Scrambled := Scrambled + Letter +', ';
    end;
  ShowMessage('UnSorted - '+Scrambled);
end;
Jerry Mallett
  • 75
  • 1
  • 6