Generate Random numbers without Duplicates from List in C# – Programming, Pseudocode Example, C# Programming Example
Collection List

Generate Random numbers without Duplicates from List in C#

In this example you will learn how to make “Random number generator with no duplicates from a list ” in C# language.

Lastday I was faced with the task of getting as many random elements as possible from a list. In this case, no random element should be returned twice. For this I have written a generic extension method. In the extension method, all elements are mixed first. Then random items are fetched from the mixed list with a for loop.
If there are fewer elements in the list than requested, then only as many elements are returned as there are.

Here is a sample call:

The output would be for example:

4 3 1 7 8 2 9 5

Here is solution:

 

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: