Anyone know C# can help make this to PB

Just starting out? Need help? Post your questions and find answers here.
vwidmer
Enthusiast
Enthusiast
Posts: 282
Joined: Mon Jan 20, 2014 6:32 pm

Anyone know C# can help make this to PB

Post by vwidmer »

Any one know C# can help put this to PB thanks

Code: Select all

public static string GenerateDeviceToken()
{
    List<int> rands = new List<int>();
    var rng = new Random();
    for (int i = 0; i < 16; i++)
    {
        var r = rng.NextDouble();
        double rand = 4294967296.0 * r;
        rands.Add(((int)((uint)rand >> ((3 & i) << 3))) & 255);
    }

    List<string> hex = new List<string>();
    for (int i = 0; i < 256; ++i)
    {
        hex.Add(Convert.ToString(i + 256, 16).Substring(1));
    }

    string id = "";
    for (int i = 0; i < 16; i++)
    {
        id += hex[rands[i]];

        if (i == 3 || i == 5 || i == 7 || i == 9)
        {
            id += "-";
        }
    }

    return id;
}
Sorry I dont fully understand it.
thanks
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
User avatar
Demivec
Addict
Addict
Posts: 4085
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Anyone know C# can help make this to PB

Post by Demivec »

I don't use C# but when I put the original in the blender this is what came out :) :

Code: Select all

Procedure.s GenerateDeviceToken()
  Protected Dim rands.q(15)
  Protected Dim hexValue.s(255)
  Protected id.s = "", i, r.d, rand.d
  
  ;create 15 random numbers in range $00 to $FF
  For i = 0 To 15
    r = (Random(2147483647) * 2147483647 + Random(2147483647)) / 4611686016279904256 ;generate random double 0.0 to 1.0
    rand = 4294967296.0 * r
    rands(i) = ( Int(rand) >> ((3 & i) << 3) ) & 255
  Next
  
  ;create hex string table for conversion
  For i = 0 To 255
    hexValue(i) = Right(Hex(i + 256), 2)
  Next
  
  ;build the token string
  For i = 0 To 15
    id + hexValue(rands(i)) 
    If (i = 3 Or i = 5 Or i = 7 Or i = 9)
      id + "-"
    EndIf
  Next
  
  ProcedureReturn id
EndProcedure


;test
Define x
For x = 1 To 1000
  Debug GenerateDeviceToken() ;displays values such as E3CDBEFD-F9C2-68F5-0F12-8B01C2CA86F7"
Next
It can easily be shortened to this:

Code: Select all

Procedure.s GenerateDeviceToken()
  Protected Dim rands.q(15)
  Protected id.s = "", i, r.d, rand.d
  
  ;create 15 random numbers in range $00 to $FF
  For i = 0 To 15
    r = (Random(2147483647) * 2147483647 + Random(2147483647)) / 4611686016279904256 ;generate random double 0.0 to 1.0
    rand = 4294967296.0 * r
    rands(i) = ( Int(rand) >> ((3 & i) << 3) ) & 255
  Next
  
  ;build the token string
  For i = 0 To 15
    id + Right(Hex(rands(i) + 256), 2)
    If (i = 3 Or i = 5 Or i = 7 Or i = 9)
      id + "-"
    EndIf
  Next
  
  ProcedureReturn id
EndProcedure


;test
Define x
For x = 1 To 1000
  Debug GenerateDeviceToken() ;displays values such as E3CDBEFD-F9C2-68F5-0F12-8B01C2CA86F7"
Next
@Edit: For completeness, the solutions have been reworked to function equally well on both 64-bit and 32-bit compilations to generate the desired level of randomness. Thank you Paul for alerting me to these issues dealing with Random() and the max values related to the Integer size in a compilation.
Last edited by Demivec on Fri Jun 21, 2019 9:09 am, edited 1 time in total.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Anyone know C# can help make this to PB

Post by Paul »

Your first code snippet will crash on x86 at this line...
r.d = Random(9223372036854775807) / 9223372036854775807

Help file says Random(Max) ... Max: This value needs to be positive (zero included) and may not exceed the maximum positive integer value.


Second code snippet on x86 will only return
00000000-0000-0000-0000-000000000000
Image Image
User avatar
Demivec
Addict
Addict
Posts: 4085
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Anyone know C# can help make this to PB

Post by Demivec »

Thanks for pointing that out.

Curse these 64 bit eyes of mine. It will take a moment to figure a good approach to this for 32-bit while being true to the codes intended purpose.
Last edited by Demivec on Thu Jun 20, 2019 7:21 am, edited 1 time in total.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Anyone know C# can help make this to PB

Post by BarryG »

My version:

Code: Select all

Procedure.s GenerateDeviceToken()
  For n=1 To 32
    If Random(1)=0
      t$+Chr(Random(57,48)) ; 0-9
    Else
      t$+Chr(Random(70,65)) ; A-F
    EndIf
  Next
  ProcedureReturn Left(t$,8)+"-"+Mid(t$,10,4)+"-"+Mid(t$,15,4)+"-"+Mid(t$,20,4)+"-"+Right(t$,12)
EndProcedure

For n=1 To 10
  Debug GenerateDeviceToken()
Next
vwidmer
Enthusiast
Enthusiast
Posts: 282
Joined: Mon Jan 20, 2014 6:32 pm

Re: Anyone know C# can help make this to PB

Post by vwidmer »

@Demivec @BarryG Thank you very much..

I see it isnt actually doing anything special then just making a random number. Thanks for the help.
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Anyone know C# can help make this to PB

Post by infratec »

Or the Windows API way:

Code: Select all

Procedure.s GenerateDeviceToken()
  
  Protected *UUID, UUID.GUID, Result$
  
  UuidCreate_(@UUID)
  UuidToString_(@UUID, @*UUID)
  Result$ = PeekS(*UUID)
  LocalFree_(*UUID)
  
  ProcedureReturn Result$
  
EndProcedure

For n=1 To 10
  Debug GenerateDeviceToken()
Next
vwidmer
Enthusiast
Enthusiast
Posts: 282
Joined: Mon Jan 20, 2014 6:32 pm

Re: Anyone know C# can help make this to PB

Post by vwidmer »

Is this just a standard UUID or same type of code as UUID??
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Anyone know C# can help make this to PB

Post by infratec »

It is a 'special' version, which avoids conflicts with already UUIDs in use of the used PC:

https://docs.microsoft.com/en-us/window ... uuidcreate

And ... it geerates UUIDs like all other tools which generates UUIDs :mrgreen:
Post Reply