[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Could you please convert this from PHP to DELPHI?

Posted on 2005-04-29
6
Medium Priority
?
181 Views
Last Modified: 2010-04-05
     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;
       }
0
Comment
Question by:bilgehanyildirim
6 Comments
 
LVL 19

Expert Comment

by:Barthax
ID: 13893251
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;
0
 
LVL 17

Accepted Solution

by:
geobul earned 2000 total points
ID: 13893255
Hi,

Try the following:

TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function generateRandomNumber(digits: integer): string;
var
  characters: string;
  i, randValue: integer;
begin
  characters := '0123456789';
  result := '';
  for i := 0 to digits - 1 do begin
    randValue := Random(Length(characters)-1);
    result := result + Copy(characters,randValue,1);
  end;
  // Do your check if it's exist here
end;

// usage
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(generateRandomNumber(3));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Randomize;
end;


Regards, Geo
0
 
LVL 19

Expert Comment

by:Barthax
ID: 13893267
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];
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Expert Comment

by:vadim_ti
ID: 13893279
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;
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13893294
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;
0
 

Author Comment

by:bilgehanyildirim
ID: 13893312
Thanks to all of you but geobul's answer is what I was looking for....
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question