Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a unique id

I am a student at university and our task is to create a search engine. I am having difficulty generating a unique id to assign to each url when added into the frontier. I have attempted using the SHA-256 hashing algorithm as well as Guid. Here is the code that i used to implement the guid:

public string generateID(string url_add) {     long i = 1;      foreach (byte b in Guid.NewGuid().ToByteArray())     {         i *= ((int)b + 1);     }      string number = String.Format("{0:d9}", (DateTime.Now.Ticks / 10) % 1000000000);      return number; } 
like image 277
strange_developer Avatar asked Jul 03 '12 14:07

strange_developer


People also ask

How do I generate a new GUID?

To Generate a GUID in Windows 10 with PowerShell, Type or copy-paste the following command: [guid]::NewGuid() . This will produce a new GUID in the output. Alternatively, you can run the command '{'+[guid]::NewGuid(). ToString()+'}' to get a new GUID in the traditional Registry format.


2 Answers

Why not just use ToString?

public string generateID() {     return Guid.NewGuid().ToString("N"); } 

If you would like it to be based on a URL, you could simply do the following:

public string generateID(string sourceUrl) {     return string.Format("{0}_{1:N}", sourceUrl, Guid.NewGuid()); } 

If you want to hide the URL, you could use some form of SHA1 on the sourceURL, but I'm not sure what that might achieve.

like image 144
Jaime Torres Avatar answered Oct 01 '22 16:10

Jaime Torres


Why don't use GUID?

Guid guid = Guid.NewGuid(); string str = guid.ToString(); 
like image 36
abatishchev Avatar answered Oct 01 '22 16:10

abatishchev