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.

AmigoJack
  • 5,234
  • 1
  • 15
  • 31
  • 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) : http://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. – J... Aug 23 '14 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 – user3424509 Aug 23 '14 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`. – Graymatter Aug 23 '14 at 15:36
  • 1
    @user3424509 - it's of the exact same form (32 random hex digits). – J... Aug 23 '14 at 16:59

1 Answers1

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.

Peter Kostov
  • 941
  • 1
  • 6
  • 15
  • 3
    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. http://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. – J... Aug 24 '14 at 02:49
  • For example... http://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. – J... Aug 24 '14 at 02:57
  • 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). – Peter Kostov Aug 24 '14 at 06:19
  • 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]));` – J... Aug 24 '14 at 10:26