Select N random elements from a ListT in C
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
The rand.Next(numbers.Count) generates a random index, which is then used to access a random element in the list.
Selecting N Random Elements
Method 1: Simple Iterative Selection with Replacement
The simplest method for selecting N random elements is to iterate N times and select a random index each time. This method allows for replacement, meaning the same element may appear more than once in the result set:
Method 2: Selection Without Replacement
If you want to ensure that each selected element is unique, the approach needs to be slightly different:
In this method, we create a temporary list to avoid modifying the original list. Each time an element is chosen, it is removed from the temporary list, preventing duplicates.
Performance Considerations
- Time Complexity: The simple iteration method has a time complexity of
O(N). The selection without replacement has a complexity ofO(N^2)due to theRemoveAtoperation, which can be expensive. For larger lists, consider optimizing by swapping to the end and shrinking the list size. - Space Complexity: Both methods may require additional space. The without-replacement method requires extra space for the temporary list, leading to an overall space complexity of
O(N).
Table Summary
| Method | Allows Replacement | Time Complexity | Space Complexity | Notes |
| Iterative Selection With Replacement | Yes | O(N) | O(N) | Simple but may have duplicates |
| Selection Without Replacement | No | O(N^2) | O(N) | Ensures uniqueness, slightly expensive on large lists |
Advanced Topic: Fisher-Yates Shuffle
For those requiring an efficient way of selecting elements in a random order without duplicates, the Fisher-Yates shuffle is a robust choice. It randomizes a list, from which you can then take the top N elements. Here’s an implementation:
Conclusion
Selecting random elements from a List<T> involves understanding your requirements regarding replacement and performance. By using iterative methods or the Fisher-Yates shuffle, you can efficiently achieve your goal. Always consider the trade-offs regarding time and space complexity based on the list size and operation frequency.
Related reading
- Selecting a range of items inside an array in C
- Selecting a row in DataGridView programmatically
- Send email from Elmah?
- Send HTTP POST request in .NET
- Send HTTP POST request in .NET
- Send JSON via POST in C and Receive the JSON returned?
- Sending email in .NET through Gmail
- Sending email in .NET through Gmail

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.