Add new `RandomString` routine to String Management category
Closed this issue · 5 comments
delphidabbler commented
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.
delphidabbler commented
I have this function in my local Code Snippets user database in the User Defined Snippets category.
delphidabbler commented
The Delphi RTL has a similar routine that returns a random string from an array of strings, but not from a string list.
delphidabbler commented
Implemented in develop, with unit tests, by merge commit 15c7ce2
Thaddy commented
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.
delphidabbler commented