Link to home
Start Free TrialLog in
Avatar of bilgehanyildirim
bilgehanyildirim

asked on

Could you please convert this from PHP to DELPHI?

     function generateRandomNumber($digits)
      {
              $characters = "0123456789";
                                $returnValue = "";
                                for($no=0;$no<$digits;$no++)
                               {
                                   $randValue = mt_rand(0,strlen($characters)-1);
                                   $returnValue.=substr($characters,$randValue,1);
                               }
            // Do your check if it's exist here
            return $returnValue;
       }
Avatar of Barthax
Barthax
Flag of United Kingdom of Great Britain and Northern Ireland image

I think the following is a close conversion.  Part of the problem you have with PHP is it's weak typing of variables, so I've made some basic assumptions on what types are being fed in.  Note that I haven't tested the code, so it may be a little off. :(

function generateRandomNumbers(const ANum : Integer) : Char;
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;
ASKER CERTIFIED SOLUTION
Avatar of geobul
geobul

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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];
Avatar of vadim_ti
vadim_ti

i think it would be next way:

function generateRandomNumbers(const 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;
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.GenerateRandomNumber(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;
Avatar of bilgehanyildirim

ASKER

Thanks to all of you but geobul's answer is what I was looking for....