Create three random characters in Delphi

Hi, I am trying to create three random characters using a function in Delphi, the code:
function generate(cantidad: integer): string; const letras_mi = 'abcdefghijklmnopqrstuvwxyz'; const letras_ma = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; const numeros = '0123456789'; var finalr: string; begin finalr := ''; finalr := finalr + IntToStr(Random(Length(letras_mi)) + 1); finalr := finalr + IntToStr(Random(Length(letras_ma)) + 1); finalr := finalr + IntToStr(Random(Length(numeros)) + 1); Result := finalr; end; 

the problem is that things like 20142 get me back when I'm really waiting for 3 characters of constant random variables.

+8
delphi
source share
7 answers

Your code converts integer index values โ€‹โ€‹to strings. Note that your only reference to your constants is to take their length. You are returning indices, not characters.

You can fix your code by using integer indexes that you generate to refer to elements in string constants. Mason and Ken showed how to do this.

Personally, I would end the constants and write

 Chr(ord('a') + Random(26)) 

and

 Chr(ord('a') + Random(26)) 

and

 Chr(ord('0') + Random(10)) 

The ordinal values โ€‹โ€‹of these characters were developed once again when allowing such a code.

+14
source share

See what your code does as the compiler sees:

 IntToStr(Random(Length(letras_mi)) + 1) Call IntToStr on the result of: Call Random on the result of: Add Length(letras_mi) 1 

IntToStr takes a number (e.g. 5 ) and turns it into a string (e.g. '5' ). What you want to do is use a random value for indexing in your array, for example:

 letras_mi[Random(Length(letras_mi)) + 1] 
+9
source share

You add the result of Random to finalr , not to a random letter from constants.

Try something like this - it uses Random return as an index to string constants:

 function generate(cantidad: integer): string; const letras_mi = 'abcdefghijklmnopqrstuvwxyz'; letras_ma = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; numeros = '0123456789'; begin Result := ''; Result := Result + letras_mi[Random(Length(letras_mi)) + 1]; Result := Result + letras_ma[Random(Length(letras_ma)) + 1]; Result := Result + numeros[Random(Length(numeros)) + 1]; end; 
+8
source share

An accelerated way would be to avoid reallocating memory time and again.

 function generate(cantidad: integer): string; const letras_mi = 'abcdefghijklmnopqrstuvwxyz'; numeros = '0123456789'; begin SetLength(Result, 3); // only alloc memory once Result[1] := letras_mi[Random(Length(letras_mi)) + 1]; Result[2] := UpCase(letras_mi[Random(Length(letras_mi)) + 1]); Result[3] := numeros[Random(Length(numeros)) + 1]; end; 

And sometimes itโ€™s even a little faster to use a local variable to avoid a few extra calls to UniqueString for the var-t Result parameter.

However, you need to check the timings or check the code at the processor level for one specific version of the compiler and options to see what the difference really is, if any.

 function generate(cantidad: integer): string; const letras_mi = 'abcdefghijklmnopqrstuvwxyz'; numeros = '0123456789'; var local: string; begin SetLength(local, 3); // only alloc memory once local[1] := letras_mi[Random(Length(letras_mi)) + 1]; local[2] := UpCase(letras_mi[Random(Length(letras_mi)) + 1]); local[3] := numeros[Random(Length(numeros)) + 1]; Result := local; end; 

PS. An Ord-based approach is also better than choosing a char from an array / string, but this is an independent issue. I would also be careful to use the Chr function with Delphi 2009 or newer, it will only work uniformly on the values โ€‹โ€‹# 0 .. # 127. Explicitly declared techniques like AnsiChar(i) and WideChar(i) can be more stable, because in one on a beautiful day, you will need letters outside the 7-bit subband, such as eรฑa and other European ones.

0
source share

This generates random strings that look like words.

 function GenerateRandomWord(CONST Len: Integer=16; StartWithVowel: Boolean= FALSE): string; CONST sVowels: string= 'AEIOUY'; sConson: string= 'BCDFGHJKLMNPQRSTVWXZ'; VAR i: Integer; B: Boolean; begin B:= StartWithVowel; SetLength(Result, Len); for i:= 1 to len DO begin if B then Result[i]:= sVowels[Random(Length(sVowels)) + 1] else Result[i]:= sConson[Random(Length(sConson)) + 1]; B:= NOT B; end; end; 

So use it like this: GenerateRandomWord (3);

0
source share
 function RandomString(const ALength: Integer): String; var i: Integer; LCharType: Integer; begin Result := ''; for i := 1 to ALength do begin LCharType := Random(3); case LCharType of 0: Result := Result + Chr(ord('a') + Random(26)); 1: Result := Result + Chr(ord('A') + Random(26)); 2: Result := Result + Chr(ord('0') + Random(10)); end; end; end; 
0
source share
 const Alphabetdown = 'abcdefghijklmnopqrstuvwxyz' ; procedure TForm1.Button1Click(Sender: TObject); var sGeneratedAccNo : string ; begin sGeneratedAccNo := sGeneratedAccNo + Alphabetdown[Random(Length(Alphabetdown) + 1)] ; showMessage(sGeneratedAccNo) ; 
-one
source share

All Articles