bilgehanyildirim
asked on
Could you please convert this from PHP to DELPHI?
function generateRandomNumber($digi ts)
{
$characters = "0123456789";
$returnValue = "";
for($no=0;$no<$digits;$no+ +)
{
$randValue = mt_rand(0,strlen($characte rs)-1);
$returnValue.=substr($char acters,$ra ndValue,1) ;
}
// Do your check if it's exist here
return $returnValue;
}
{
$characters = "0123456789";
$returnValue = "";
for($no=0;$no<$digits;$no+
{
$randValue = mt_rand(0,strlen($characte
$returnValue.=substr($char
}
// Do your check if it's exist here
return $returnValue;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
D'oh, forgot that Random(n) produces a 0...n-1 result, so you could change one of two lines to correct the result:
RandVal := Random(Len(Characters))+1;
- or -
result := Characters[RandVal+1];
RandVal := Random(Len(Characters))+1;
- or -
result := Characters[RandVal+1];
i think it would be next way:
function generateRandomNumbers(cons t ANum : Integer) : String;
const
Characters : String = '0123456789';
var
No : Integer;
RandVal : Integer;
begin
result := '';
for No := 1 to ANum do
begin
RandVal := Random(Len(Characters))+1;
result := result + Characters[RandVal];
end;
end;
function generateRandomNumbers(cons
const
Characters : String = '0123456789';
var
No : Integer;
RandVal : Integer;
begin
result := '';
for No := 1 to ANum do
begin
RandVal := Random(Len(Characters))+1;
result := result + Characters[RandVal];
end;
end;
So your trying to generate a string of numbers, where the number of digits in length is whatever you pass in as $digits
What is a bit confusing is that you are looping x number of times depending on the size of $digits, but it seems you are only returning the last value retrieved from the loop.
Is it meant to add a number to the return value everytime it goes round the loop and then pass back the final string?
Anyway here is something to be getting on with
procedure TForm1.Button1Click(Sender : TObject);
var
sDisplay: string;
begin
// start random generator
Randomize;
// get the result you need
sDisplay := GenerateRandomNumber(5);
// show the result for now
ShowMessage(sDisplay);
end;
function TForm1.GenerateRandomNumbe r(iLen: Integer): string;
var
iLoop, iRand: Integer;
begin
// clear
Result := '';
// you are only doing from 0 to 9 anyway
for iLoop := 0 to iLen do
begin
// get a random number between 0 and 9
iRand := Trunc(Random(10));
// add the result to the return string
Result := Result + IntToStr(iRand);
end;
end;
What is a bit confusing is that you are looping x number of times depending on the size of $digits, but it seems you are only returning the last value retrieved from the loop.
Is it meant to add a number to the return value everytime it goes round the loop and then pass back the final string?
Anyway here is something to be getting on with
procedure TForm1.Button1Click(Sender
var
sDisplay: string;
begin
// start random generator
Randomize;
// get the result you need
sDisplay := GenerateRandomNumber(5);
// show the result for now
ShowMessage(sDisplay);
end;
function TForm1.GenerateRandomNumbe
var
iLoop, iRand: Integer;
begin
// clear
Result := '';
// you are only doing from 0 to 9 anyway
for iLoop := 0 to iLen do
begin
// get a random number between 0 and 9
iRand := Trunc(Random(10));
// add the result to the return string
Result := Result + IntToStr(iRand);
end;
end;
ASKER
Thanks to all of you but geobul's answer is what I was looking for....
function generateRandomNumbers(cons
const
Characters : String = '0123456789';
var
No : Integer;
RandVal : Integer;
begin
for No := 0 to ANum do
begin
RandVal := Random(Len(Characters));
result := Characters[RandVal];
end;
end;