delphidabbler / code-snippets

Collections of reusable code snippets, mainly in Pascal.
Other
30 stars 2 forks source link

Add new `RandomString` routine to String Management category #28

Open delphidabbler opened 3 weeks ago

delphidabbler commented 3 weeks ago

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 3 weeks ago

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