-1

A random Id was created.

How can I make it unique and check for uniqueness?

Id is generated in RNGCryptoServiceProvider, it creates only a random id but I need a unique one.

private const string Characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
private static readonly RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
Elikill58
  • 4,050
  • 24
  • 23
  • 45
Oolga
  • 11
  • 1
  • 3
    Unique one comparing to? How can you know if it's unique or not? – Elikill58 Jun 08 '23 at 08:26
  • You typically assume that a random ID of big length is unique enough, at least for the lifespan of your system it's very improbable to hit the same ID twice. – Alexey S. Larionov Jun 08 '23 at 08:29
  • Unique compared to the previous generated id. Which is not repeated when generating id. I have a short id – Oolga Jun 08 '23 at 08:30
  • 2
    According to *birthday paradox* if you generate random number out of `n` possible, the first collision will be at about `sqrt(2 * n)`, so you can estimate if you are going to have collisions and should you try to prevent them. In case of guid, `n = 2 ^ 112`, so we might estimate ~ `sqrt(2 ^ 113) ~ 1e17` guids generated before first collision – Dmitry Bychenko Jun 08 '23 at 08:33
  • Since you can't remember all the generated IDs (too much memory required), you can try to implement a [Bloom filter](https://en.wikipedia.org/wiki/Bloom_filter) to track previously generated IDs. It's doesn't guarantee the uniqueness, but with high probability it can answer if the ID is unique without costly lookup and without too much memory consumed – Alexey S. Larionov Jun 08 '23 at 08:33
  • Please note that the `RNGCryptoServiceProvider` class is obsolete as of .NET 7. Refer to [RNGCryptoServiceProvider Class](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.rngcryptoserviceprovider?view=net-7.0). It says *To generate a random number, use one of the RandomNumberGenerator static methods instead.* – Mustafa Özçetin Jun 08 '23 at 08:38
  • 1
    Not sure what you want to achieve but a more simple way might be to use a `System.Guid`. – Mustafa Özçetin Jun 08 '23 at 08:41
  • 1
    why not just use incrementing ids, 1, 2, 3, ... ? Then you don't need any checking for uniqueness... That's in fact what most dbms also do, they have some autoincrement-field which is just incremented whenever a new row is inserted into the table. – MakePeaceGreatAgain Jun 08 '23 at 08:49
  • 1
    Generate a date with millisecond precision `DateTime.Now.ToString("yyyyMMddHHmmssfff")` and it will be unique within that range – giorgi02 Jun 08 '23 at 08:51

1 Answers1

0

In c#, you can generate unique ids using the guid class.

Guid uniqueId = Guid.NewGuid();
string uniqueIdString = uniqueId.ToString();

To check the uniqueness of an id, you may need to store the generated ones and compare them with the new ones you generate using a hashset like this:

HashSet<Guid> idSet = new HashSet<Guid>();

Guid newId = Guid.NewGuid();
    if (idSet.Contains(newId))
{
    Console.WriteLine("ID is not unique :(");
}
else
{
    idSet.Add(newId);
    Console.WriteLine("ID is unique :)");
}
Ali Safari
  • 1,535
  • 10
  • 19