Why wouldn't it work? After all, a string is just an array of chars.
Printable View
But in practical use, it interprets it as a literal array of chars, and not as a string, so it gives you a type mismatch.
Simba Code:program new;
function RandomKey(Len: integer): string;
var
I: integer;
begin
SetLength(Result, Len);
for I := 1 to Len do
Result[I] := chr(33 + Random(93));
end;
begin
Writeln(RandomKey(5));
end.
Works for me.Code:Compiled succesfully in 15 ms.
Wf|#!
Successfully executed.
wat
Swore it gave me an error before..
Either way, mine was a single line shorter. :3
cool, you learn something new everyday haha
Nope Try this
Simba Code:program new;
{*******************************************************************************
function RandomKey(Len: integer): string;
By: Dgby714, Sandstorm, Smartzkid
Description: Returns a random string with Len Length
*******************************************************************************}
function RandomKey(Len: Integer): string;
var
i: Integer;
begin
for i := 0 to Len - 1 do
Result := Result + chr(33 + Random(93));
end;
var
Test: string;
begin
Test := 'Dgby714 rocks * '
Test := RandomKey(12);
WriteLn(Test);
end.
Output:
Code:Compiled succesfully in 16 ms.
Dgby714 rocks * qDpmw:/pFMmJ
Successfully executed.
;) I know.
It makes sense, I'm just confus on why I thought it didn't work before.
Moved as requested/also part of the cleaning up of tutorial Island.
~BraK
I know this is a very old thread, but @Dgby, have you put all these funcs into a single file so they can be used as an include?