It is currently Sat Oct 19, 2019 2:17 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Anyone know C# can help make this to PB
PostPosted: Thu Jun 20, 2019 2:05 am 
Offline
Enthusiast
Enthusiast

Joined: Mon Jan 20, 2014 6:32 pm
Posts: 241
Any one know C# can help put this to PB thanks

Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Anyone know C# can help make this to PB
PostPosted: Thu Jun 20, 2019 3:10 am 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3585
Location: Utah, USA
I don't use C# but when I put the original in the blender this is what came out :) :
Code:
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:
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.

_________________
Image


Last edited by Demivec on Fri Jun 21, 2019 9:09 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Anyone know C# can help make this to PB
PostPosted: Thu Jun 20, 2019 6:09 am 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Fri Apr 25, 2003 4:34 pm
Posts: 911
Location: Canada
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Anyone know C# can help make this to PB
PostPosted: Thu Jun 20, 2019 6:39 am 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3585
Location: Utah, USA
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.

_________________
Image


Last edited by Demivec on Thu Jun 20, 2019 7:21 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Anyone know C# can help make this to PB
PostPosted: Thu Jun 20, 2019 6:54 am 
Offline
Enthusiast
Enthusiast

Joined: Thu Apr 18, 2019 8:17 am
Posts: 333
My version:

Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Anyone know C# can help make this to PB
PostPosted: Thu Jun 20, 2019 12:06 pm 
Offline
Enthusiast
Enthusiast

Joined: Mon Jan 20, 2014 6:32 pm
Posts: 241
@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


Top
 Profile  
Reply with quote  
 Post subject: Re: Anyone know C# can help make this to PB
PostPosted: Thu Jun 20, 2019 12:17 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 4361
Location: Germany
Or the Windows API way:
Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Anyone know C# can help make this to PB
PostPosted: Thu Jun 20, 2019 4:24 pm 
Offline
Enthusiast
Enthusiast

Joined: Mon Jan 20, 2014 6:32 pm
Posts: 241
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Anyone know C# can help make this to PB
PostPosted: Thu Jun 20, 2019 5:50 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 4361
Location: Germany
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:


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: Baidu [Spider], STARGÅTE and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye