delphidabbler/code-snippets

Add new `RandomString` routine to String Management category

Closed this issue · 5 comments

Suggest new routine, with following attributes:

Decription

Returns a random string from the given string list, which must not be empty.

An ERangeError exception is raised if the string list is empty.

Source code

function RandomString(const SL: TStrings): string;
begin
  if SL.Count = 0 then
    raise ERangeError.Create('RandomString called with empty string list');
  Result := SL[Random(SL.Count)];
end;

Snippet type

Routine.

Category

String Management

Required units

SysUtils, Classes.

Required snippets

None.

XRefs

None.

Extra

None.

I have this function in my local Code Snippets user database in the User Defined Snippets category.

The Delphi RTL has a similar routine that returns a random string from an array of strings, but not from a string list.

Implemented in develop, with unit tests, by merge commit 15c7ce2

Why not do the whole list?

{$apptype console}{$H+}
uses classes;
var
  list:TStringlist;
  i:integer;
begin
  list := TStringList.create;
  list.add('test1');
  list.add('test2');
  list.add('test3');
  list.add('test4');
  list.add('test5');
  list.add('test6');
  list.add('test7');
  list.add('test8');
  list.add('test9');
  list.add('test0');
//  randomize; // optional
  list.CustomSort(function(l:TStringList;a,b:integer):integer
                  begin
                    { becomes -1, 0 or 1 }
                    result := Random(3)-1;
                  end);
  Writeln(list.text);
  list.free;
end.

In effect Fisher-Yates shuffle (variant Sattolo.?) Usable on any type list.

@Thaddy - nice, I like that.

I'll add a new function, RandomiseStrings (?) that uses your algorithm. I'll leave the decision about whether to call Randomize call to the user.

I've opened a new issue, including some possible implementations, to remind me to do this: see issue #64.