3

I'm trying to convert JavaScript code to Delphi but I failed. Javascript:

/* generate random progress-id */
var uuid = "";
var i;
for (i = 0; i < 32; i++) {
  uuid += Math.floor(Math.random() * 16).toString(16);
}

...and its output:

a638aa8f74e2654c725fd3cdcf2927d3

My try in Delphi:

function uid: String;
var
  i: Integer;
begin
  for I := 0 to 31 do begin
    result := result + IntToStr(Floor(Random * 16));
  end;
end;

My knowledge in Delphi is limited, so I don't know what to do more. I would like to see some help and learn from it.

4
  • 1
    If you're trying to generate a guid, there are better ways to do it (ie: using the built in functions for that purpose) : stackoverflow.com/a/2300706/327083 You can strip out the hyphens and lowercase it with basic string functions if you need it to be in whatever format. Commented Aug 23, 2014 at 14:58
  • No it's not a guid , it's a random progress-id , i need to send it as a post data to somesite Commented Aug 23, 2014 at 15:05
  • 2
    It looks like the toString(16) in javascript converts to a hex value. In that case, you should use 'IntToHex' instead of IntToStr. Commented Aug 23, 2014 at 15:36
  • 1
    @user3424509 - it's of the exact same form (32 random hex digits). Commented Aug 23, 2014 at 16:59

1 Answer 1

7

Literally, here is how the function looks in delphi:

function uid: String;
var
  i: Integer;
begin
  for i := 0 to 31 do
    Result := Result + IntToHex(Random(16), 1);
end;

If you need a lowercased "id" - use the AnsiLowerCase function.

EDIT

In the name of correctness, the homebrew method from above is not recommended- it's just a literally translation of the javascript snippet. It could lead to collisions (and will).

The following function is recommended:

function uid: String;
var
  myGuid: TGUID;
begin
  if Succeeded(CreateGUID(myGUID)) then
    Result := Format('%0.8X%0.4X%0.4X%0.2X%0.2X%0.2X%0.2X%0.2X%0.2X%0.2X%0.2X',
      [myGUID.D1, myGUID.D2, myGUID.D3,
      myGUID.D4[0], myGUID.D4[1], myGUID.D4[2], myGUID.D4[3],
      myGUID.D4[4], myGUID.D4[5], myGUID.D4[6], myGUID.D4[7]]) else
    {TODO: some error processing - something bad happened}
end;

The notice for the lowercased "id" from above is valid here too.

Sign up to request clarification or add additional context in comments.

4 Comments

It should be stressed that, without any other calls, this function will produce the same pseudo-random number sequences each time the program is run. Naturally, for generating UIDs of any sort this is catastrophically bad. A call to Randomize must be made somewhere in the program to initialize the random number generator with a new seed. docwiki.embarcadero.com/Libraries/en/System.Randomize Using the purpose-built function CreateGuid is altogether a better idea since it is not susceptible to the myriad faults that can be introduced by half-baked roll-your-own solutions like this.
For example... stackoverflow.com/q/6906916/327083 GUID collisions are all too common when using naive homebrew approaches. The WinAPI provides this service for free, one has to think it is for good reason.
I completely agree with you. In a matter of fact, the first thing I thought is the use of CreateGUID. But that would lead to converting it to string, parsing it to show it in a proper way as the author wants. I thought that way (although correct) will lead to confusion. Also, the author didn't specify the whole picture of the "random progress-id" need. Also he denied that his UID is a GUID one (that is obviously not true). So, because the question (convert some code..) and the insufficient information provided, I decided to answer literally (as it stands at the top of my A).
Agreed, your original answer did correctly answer OP's question - it's still important to think about the intended purpose. Another way to generate the string in OP's desired format (assuming lowercase is by design) is simply Lowercase(StringReplace(GuidToString(myGuid), '-', '', [rfReplaceAll]));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.