Amazon.com Widgets Fun Code of the Week #2

Fun Code of the Week #2

By Nick at November 07, 2011 15:51
Filed Under: Delphi, Fun Code, Software Development
function RandomString(aLength: Integer; aInputChars: string): string;
begin
  Result := '';
  if Length(aInputChars) <= 0 then
  begin
    Exit;
  end;
  Randomize;

  repeat
    Result := Result + aInputChars[Random(Length(aInputChars)) + 1];
  until (Length(Result) = aLength);
end;

Comments (9) -

11/7/2011 4:23:54 PM #

Julian M Bucknall

Nick

Hate to say it, but call this two or more times in quick succession and you get the same string returned. Not as random as first thought... It's that evil Randomize.

Cheers, Julian

Julian M Bucknall United States |

11/7/2011 4:41:42 PM #

Nick Nobody

A lot of (premature) optimisations done:

function RandomString(aLength: Integer; aInputChars: string): string;
var
  LLenInputChars : Integer;
  lp : Integer;
begin
  Result := '';

  LLenInputChars := Length(aInputChars);

  if LLenInputChars = 0 then
    Exit;

  Randomize;

  SetLength(Result, aLength);

  for lp := 1 to aLength do
    Result[lp] := aInputChars[Random(LLenInputChars) + 1];
end;

Nick Nobody Australia |

11/8/2011 3:42:38 AM #

Martijn Coppoolse

While you're prematurely optimizing, why not make the parameters const while you're at it?
(Especially since you're not reusing them anyway).

Martijn Coppoolse Netherlands |

11/8/2011 12:21:17 AM #

volvox

This or any function random are not so random... in fact they acts randomly as they are programed to act. Ex:  cod.ifies.com/.../...-windows-openssl-rand-on.html

volvox Canada |

11/8/2011 12:37:31 AM #

volvox

This and other function random are not so 'random'... in fact they act randomly as they are programed to act: ex: cod.ifies.com/.../...-windows-openssl-rand-on.html

volvox Canada |

11/8/2011 3:20:05 AM #

Stefan Glienke

Randomize should only be called once (typically when starting the application or in some initialization part of the unit) and not on every call of the method.

Stefan Glienke Germany |

11/8/2011 7:29:34 AM #

Dean Hill

function RandomString(aLength: Integer; aInputChars: string): string;
var
  LLenInputChars : Integer;
begin
  Result := '';

  if (aLength = 0) then
    Exit;

  LLenInputChars := Length(aInputChars);

  if LLenInputChars = 0 then
    Exit;

  Randomize;

  Result := aInputChars[Random(LLenInputChars) + 1] + RandomString(aLength - 1, aInputChars);
end;

Smile

Dean Hill South Africa |

11/8/2011 7:30:21 AM #

Dean Hill

Without the Randomize.

Dean Hill South Africa |

11/8/2011 1:11:39 PM #

Nick Hodges

Recursion!  Awesome!

Nick Hodges United States |

Comments are closed

My Book

A Pithy Quote for You

"Good judgement is the result of experience ... Experience is the result of bad judgement."    –  Fred Brooks

Amazon Gift Cards

General Disclaimer

The views I express here are entirely my own and not necessarily those of any other rational person or organization.  However, I strongly recommend that you agree with pretty much everything I say because, well, I'm right.  Most of the time. Except when I'm not, in which case, you shouldn't agree with me.

Month List