Languages
[Edit]
EN

C#/.NET - generate unique random numbers

12 points
Created by:
Root-ssh
178260

There is couple of ways to generate unique random numbers in C#/.NET.

1. Array with custom shuffling example

Edit

Example:

Output:

2. Array.Sort method based example

Edit

Example:

Output:

3 Enumerable.Range + Enumerable.OrgedBy methods based example

Edit

Example:

Output:

4. HashSet class based example

Edit

Example:

Output:

Note: generating random unique values with HashSet could take in most worse case much more iterations than number of expected numbers.

References

Edit
  1. Array.Sort Method - Microsoft Docs
  2. Enumerable.OrderBy Method - Microsoft Docs
  3. HashSet<T> Class - Microsoft Docs
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

C# / .NET - random

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join