PHP equivalent to uniqid () in C #
trying to duplicate some PHP code in C #. The PHP code uses the uniqid function with a larger entropy parameter of true. Any idea on the best way to duplicate this in C #?
http://us2.php.net/uniqid
something like:
string uniqid(string prefix, bool more_entropy) {
if(string.IsNullOrEmpty(prefix))
prefix = string.Empty;
if (!more_entropy) {
return (prefix + System.Guid.NewGuid().ToString()).Left(13);
} else {
return (prefix + System.Guid.NewGuid().ToString() + System.Guid.NewGuid().ToString()).Left(23);
}
}
Edit: display comments
This is very easy off the head of my solution, and I would suggest that for a more rigorous solution, you investigate the correct "random" number generation, for example through the System.Math namespace. Please check out these other SO questions and answers about random number generation .
System.Guid.NewGuid (). ToString ()
You will get a unique string.
After some googling I found how PHP uniqid works and implemented it in C #:
private string GetUniqID()
{
var ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
double t = ts.TotalMilliseconds / 1000;
int a = (int)Math.Floor(t);
int b = (int)((t - Math.Floor(t)) * 1000000);
return a.ToString("x8") + b.ToString("x5");
}
This code provides the same result, the only difference is that the additional parameters to uniqid () are not implemented.