Recent

Author Topic: [Solved] How to shuffle letters  (Read 818 times)

nikel

  • Full Member
  • ***
  • Posts: 218
[Solved] How to shuffle letters
« on: December 03, 2023, 01:31:41 pm »
Hello, I'm trying to shuffle letters in Memo box. But random isn't working. Here's my code:

Code: Pascal  [Select][+][-]
  1. Randomize;
  2. ...
  3. for I:=0 to Text_Mmo.Lines.Count do
  4. begin
  5.   StrPtr^:=Token^.GetTok(Text_Mmo.Lines[I], I + 1, ' ');
  6.   Len^:=Length(StrPtr^);
  7.  
  8.   if (Len^ >= 2) then
  9.   begin
  10.     SetLength(Arr, Len^);
  11.  
  12.     // Array ata
  13.     for J:=0 to Len^ do
  14.     begin
  15.       Arr[J + 1]:=StrPtr^[J];
  16.     end;
  17.  
  18.     for J:=1 to Len^ do
  19.     begin
  20.       RandNum^:=Random(Len^);
  21.       // ShowMessage(RandNum^.ToString);
  22.       while RandNum^ = J do
  23.       begin
  24.         RandNum^:=Random(Len^);
  25.       end;
  26.       Arr[J]:=StrPtr^[RandNum^ - 1];
  27.     end;
  28.     ShowMessage(ArrayToString(Arr)); // This outputs Hello and then world!
  29.   end;
  30. end;

Code: Pascal  [Select][+][-]
  1. function TForm1.ArrayToString(charArray: array of char): string;
  2. var
  3.   I        : Integer;
  4. begin
  5.   // Result := 'a';
  6.   for I := Low(CharArray) to High(CharArray) do
  7.     Result := Result + CharArray[I];
  8. end;

How can I randomize a char of array?
« Last Edit: December 03, 2023, 02:11:47 pm by nikel »

paweld

  • Hero Member
  • *****
  • Posts: 1268
Re: How to shuffle letters
« Reply #1 on: December 03, 2023, 01:53:37 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Randomize;
  4. end;
  5.  
  6. procedure TForm1.Button1Click(Sender: TObject);
  7. begin
  8.   ShowMessage(RandomChars(Memo1.Lines.Text));
  9. end;
  10.  
  11. function TForm1.RandomChars(s: String): String;
  12. var
  13.   i, len: Integer;
  14.   ws: WideString;
  15. begin
  16.   Result := '';
  17.   ws := WideString(s);
  18.   len := Length(ws);
  19.   while len > 1 do
  20.   begin
  21.     i := Random(len) + 1;
  22.     Result := Result + String(ws[i]);
  23.     Delete(ws, i, 1);
  24.     Dec(len);
  25.   end;
  26.   if len = 1 then
  27.     Result := Result + ws;
  28. end;    
Best regards / Pozdrawiam
paweld

nikel

  • Full Member
  • ***
  • Posts: 218
Re: How to shuffle letters
« Reply #2 on: December 03, 2023, 02:11:26 pm »
Thank you for your reply. Your code is so nice.

Thaddy

  • Hero Member
  • *****
  • Posts: 16196
  • Censorship about opinions does not belong here.
Re: [Solved] How to shuffle letters
« Reply #3 on: December 03, 2023, 02:24:10 pm »
This is better code, uses the well known Sattolo cycle:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$apptype console}
  2. uses math;
  3. var
  4.   a:Array[0..25] of  char='abcdefghijklmnopqrstuvwxyz';
  5.   i,j:integer;
  6.   t:char;
  7. begin
  8.   randomize;
  9.   i := length(a);
  10.   while i >1 do  
  11.   begin
  12.     dec(i);
  13.     j :=randomrange(0,i);
  14.     t:=a[i];a[i]:=a[j];a[j]:=t;
  15.     write(a[i]:2);
  16.   end;
  17.   writeln;
  18. end.
« Last Edit: December 03, 2023, 02:26:08 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018