Select N random elements from a ListT in C
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
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 courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.