UniqueIdentifier
The purpose of this class is to create unique identifiers. In this way, it allows us to create labels that identify different things such as goods and objects in various places.
Members
private readonly Random _random;
It holds the random class that will ensure randomness.
private readonly HashSet<uint> _usedIds;
It keeps the used ids in a HasSet so that we know the previously used ids.
private readonly _idLenght;
It holds the length information of the id to be created.
Methods
It allows us to specify the length of the id when creating the class.
It is checked whether the sent ID is in the list or not.
If the value is in the list, it returns false.
It creates a unique ID and if this ID is in the list, it creates it again.
If the ID is not in the list, it adds it to the _usedIds list and returns the Id.
Fisher-Yates Suffle algorithm was used for randomness.
A list is created for all numbers between 0 and 10. (0..10)
The length of the digits is assigned to a variable (n) and a loop is run until this variable becomes 0.
n is decreased at each stage. A random number up to n+1 is generated and assigned to a variable named k.
Then, the number in the list of digits corresponding to k is replaced by the number in the list of digits corresponding to n.
thus the numbers are scrambled.
Then a for loop is started for the length of _idLenght.
Each time, the uniqueID variable is multiplied by 10 and the element at index i in the digits list is added.
and this value is returned.
Last updated