Skip to content

Add new RandomString routine to String Management category #28

Open
@delphidabbler

Description

@delphidabbler

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.

Activity

delphidabbler

delphidabbler commented on Oct 14, 2024

@delphidabbler
OwnerAuthor

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

delphidabbler

delphidabbler commented on Jan 5, 2025

@delphidabbler
OwnerAuthor

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

added
acceptedIssue will be actioned
and removed
consideringIssue is currently under consideration
on Apr 5, 2025
moved this from Considering to Accepted in Code Snippetson Apr 5, 2025
added a commit that references this issue on Apr 5, 2025
15c7ce2
added
completedIssue completed and committed to develop. To be closed on next release
and removed
acceptedIssue will be actioned
on Apr 5, 2025
moved this from Accepted to Completed in Code Snippetson Apr 5, 2025

2 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

completedIssue completed and committed to develop. To be closed on next releaseenhancementNew feature or request

Projects

Status

Completed

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @delphidabbler

      Issue actions

        Add new `RandomString` routine to String Management category · Issue #28 · delphidabbler/code-snippets