Skip to content

Add RandomFrom overloads #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
delphidabbler opened this issue Jul 23, 2023 · 4 comments
Closed

Add RandomFrom overloads #21

delphidabbler opened this issue Jul 23, 2023 · 4 comments
Assignees
Labels
enhancement New feature or request rejected This will not be worked on

Comments

@delphidabbler
Copy link
Owner

delphidabbler commented Jul 23, 2023

There are RandomFrom functions in System.Math for all the Float array types + Integer, Int64 and UInt64 arrays.

There also RandomFrom functions in System.AnsiStrings and System.StrUtils for arrays of ANSI and Unicode strings.

What's missing are RandomFrom overloads for Cardinal arrays (+ shorter Integer types), TBytes and TStrings.

@delphidabbler delphidabbler self-assigned this Jul 23, 2023
@delphidabbler delphidabbler added the accepted Issue will be actioned label Jul 23, 2023
@github-project-automation github-project-automation bot moved this to Considering in Code Snippets Jul 23, 2023
@delphidabbler delphidabbler moved this from Considering to Accepted in Code Snippets Jul 23, 2023
@delphidabbler delphidabbler added the enhancement New feature or request label Jul 23, 2023
@delphidabbler
Copy link
Owner Author

I've created a TStrings RandomFrom overload in CodeSnip. Here's the source:

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

@delphidabbler
Copy link
Owner Author

What's missing are RandomFrom overloads for Cardinal arrays (+ shorter Integer types), TBytes and TStrings.

Note that Delphi 12 now has the following overloads, so we don't need the Cardinal and TBytes overloads anymore:

function RandomFrom(const AValues: array of ShortInt): ShortInt; overload;
function RandomFrom(const AValues: array of SmallInt): SmallInt; overload;
function RandomFrom(const AValues: array of Integer): Integer; overload;
function RandomFrom(const AValues: array of Int64): Int64; overload;
function RandomFrom(const AValues: array of Byte): Byte; overload;
function RandomFrom(const AValues: array of Word): Word; overload;
function RandomFrom(const AValues: array of Cardinal): Cardinal; overload;
function RandomFrom(const AValues: array of UInt64): UInt64; overload;
function RandomFrom(const AValues: array of Single): Single; overload;
function RandomFrom(const AValues: array of Double): Double; overload;
function RandomFrom(const AValues: array of Extended): Extended; overload;
function RandomFrom(const AValues: array of Boolean): Boolean; overload;

@delphidabbler
Copy link
Owner Author

I've created a TStrings RandomFrom overload in CodeSnip. Here's the source:

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

This routine has its own issue: #28.

@delphidabbler
Copy link
Owner Author

There are so many overloads of RandomFrom in the Delphi RTL that it really isn't worth implementing any more. Furthermore, the proposed RandomString function in issue #28 means there is no need for a string list overload of RandomFrom.

Therefore this issue is being rejected and closed.

@delphidabbler delphidabbler added rejected This will not be worked on and removed accepted Issue will be actioned labels Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request rejected This will not be worked on
Projects
None yet
Development

No branches or pull requests

1 participant