Open
Description
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.
Metadata
Metadata
Assignees
Projects
Status
Completed
Milestone
Relationships
Development
No branches or pull requests
Activity
delphidabbler commentedon Oct 14, 2024
I have this function in my local Code Snippets user database in the User Defined Snippets category.
delphidabbler commentedon Jan 5, 2025
The Delphi RTL has a similar routine that returns a random string from an array of strings, but not from a string list.
RandomFrom
overloads #21Merge branch 'feature/28-add-RandomString-function' into develop
2 remaining items