PHP equivalent to uniqid () in C #
2 answers
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.
+8
source to share
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.
+6
source to share