Skip to content

Instantly share code, notes, and snippets.

@femtotrader
Created January 3, 2015 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save femtotrader/0b4f7a2db601826f6240 to your computer and use it in GitHub Desktop.
Save femtotrader/0b4f7a2db601826f6240 to your computer and use it in GitHub Desktop.
Universally Unique IDentifier (UUID) URN Namespace for Metatrader
//+------------------------------------------------------------------+
//| uuid.mqh |
//| Universally unique identifier |
//| Copyright © 2014, FemtoTrader |
//| https://sites.google.com/site/femtotrader/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2014, FemtoTrader"
#property link "https://sites.google.com/site/femtotrader/"
#property version "1.00"
#property strict
//http://en.wikipedia.org/wiki/Universally_unique_identifier
//RFC 4122
// A Universally Unique IDentifier (UUID) URN Namespace
// http://tools.ietf.org/html/rfc4122.html
//+------------------------------------------------------------------+
//|UUID Version 4 (random) |
//|Version 4 UUIDs use a scheme relying only on random numbers. |
//|This algorithm sets the version number (4 bits) as well as two |
//|reserved bits. All other bits (the remaining 122 bits) are set |
//|using a random or pseudorandom data source. Version 4 UUIDs have |
//|the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx |
//|where x is any hexadecimal digit and y is one of 8, 9, A, or B |
//|(e.g., f47ac10b-58cc-4372-a567-0e02b2c3d479). |
//+------------------------------------------------------------------+
string uuid4()
{
string alphabet_x="0123456789abcdef";
string alphabet_y="89ab";
string id="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"; // 36 char = (8-4-4-4-12)
ushort character;
for(int i=0; i<36; i++)
{
if(i==8 || i==13 || i==18 || i==23)
{
character='-';
}
else if(i==14)
{
character='4';
}
else if(i==19)
{
character = (ushort) MathRand() % 4;
character = StringGetChar(alphabet_y, character);
}
else
{
character = (ushort) MathRand() % 16;
character = StringGetChar(alphabet_x, character);
}
id=StringSetChar(id,i,character);
}
return (id);
}
//+------------------------------------------------------------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment