Skip to main content
deleted 9 characters in body
Source Link
IEatBagels
  • 12.7k
  • 3
  • 48
  • 99

Instead of using a List<int>, you should use an HashSet<int>. The HashSet<> prohibites multiple identical values. And the Add method returns a bool that indicates if the element was added to the list, this way you could change this code :

public static List<int> GetRandomNumbers(int count) { List<int> randomNumbers = new List<int>(); for (int i=0; i<count; i++) { int number; do number = random.Next(); while (randomNumbers.Contains(number)); randomNumbers.Add(number); } return randomNumbers; } 

to this :

public static IEnumerable<int> GetRandomNumbers(int count) { HashSet<int> randomNumbers = new HashSet<int>(); for (int i = 0; i < count; i++) while (!randomNumbers.Add(random.Next())); return randomNumbers.ToList();randomNumbers; } 

Note that I changed the return value from List<int> to IEnumerable<int>, if you don't plan to add/remove values from your list, you should return IEnumerable<int>, if you plan to do it, return ICollection<int>. You shouldn't return a List<int> because it is an implementation detail and that the List<> isn't made to be extensible.

Instead of using a List<int>, you should use an HashSet<int>. The HashSet<> prohibites multiple identical values. And the Add method returns a bool that indicates if the element was added to the list, this way you could change this code :

public static List<int> GetRandomNumbers(int count) { List<int> randomNumbers = new List<int>(); for (int i=0; i<count; i++) { int number; do number = random.Next(); while (randomNumbers.Contains(number)); randomNumbers.Add(number); } return randomNumbers; } 

to this :

public static IEnumerable<int> GetRandomNumbers(int count) { HashSet<int> randomNumbers = new HashSet<int>(); for (int i = 0; i < count; i++) while (!randomNumbers.Add(random.Next())); return randomNumbers.ToList(); } 

Note that I changed the return value from List<int> to IEnumerable<int>, if you don't plan to add/remove values from your list, you should return IEnumerable<int>, if you plan to do it, return ICollection<int>. You shouldn't return a List<int> because it is an implementation detail and that the List<> isn't made to be extensible.

Instead of using a List<int>, you should use an HashSet<int>. The HashSet<> prohibites multiple identical values. And the Add method returns a bool that indicates if the element was added to the list, this way you could change this code :

public static List<int> GetRandomNumbers(int count) { List<int> randomNumbers = new List<int>(); for (int i=0; i<count; i++) { int number; do number = random.Next(); while (randomNumbers.Contains(number)); randomNumbers.Add(number); } return randomNumbers; } 

to this :

public static IEnumerable<int> GetRandomNumbers(int count) { HashSet<int> randomNumbers = new HashSet<int>(); for (int i = 0; i < count; i++) while (!randomNumbers.Add(random.Next())); return randomNumbers; } 

Note that I changed the return value from List<int> to IEnumerable<int>, if you don't plan to add/remove values from your list, you should return IEnumerable<int>, if you plan to do it, return ICollection<int>. You shouldn't return a List<int> because it is an implementation detail and that the List<> isn't made to be extensible.

deleted 144 characters in body
Source Link
IEatBagels
  • 12.7k
  • 3
  • 48
  • 99

The @Jeroen's answer is the one you should take, but I wanted to review something in your code anyway, in order to make you learn something!

Instead of using a List<int>, you should use an HashSet<int>. The HashSet<> prohibites multiple identical values. And the Add method returns a bool that indicates if the element was added to the list, this way you could change this code :

public static List<int> GetRandomNumbers(int count) { List<int> randomNumbers = new List<int>(); for (int i=0; i<count; i++) { int number; do number = random.Next(); while (randomNumbers.Contains(number)); randomNumbers.Add(number); } return randomNumbers; } 

to this :

public static IEnumerable<int> GetRandomNumbers(int count) { HashSet<int> randomNumbers = new HashSet<int>(); for (int i = 0; i < count; i++) while (!randomNumbers.Add(random.Next())); return randomNumbers.ToList(); } 

Note that I changed the return value from List<int> to IEnumerable<int>, if you don't plan to add/remove values from your list, you should return IEnumerable<int>, if you plan to do it, return ICollection<int>. You shouldn't return a List<int> because it is an implementation detail and that the List<> isn't made to be extensible.

The @Jeroen's answer is the one you should take, but I wanted to review something in your code anyway, in order to make you learn something!

Instead of using a List<int>, you should use an HashSet<int>. The HashSet<> prohibites multiple identical values. And the Add method returns a bool that indicates if the element was added to the list, this way you could change this code :

public static List<int> GetRandomNumbers(int count) { List<int> randomNumbers = new List<int>(); for (int i=0; i<count; i++) { int number; do number = random.Next(); while (randomNumbers.Contains(number)); randomNumbers.Add(number); } return randomNumbers; } 

to this :

public static IEnumerable<int> GetRandomNumbers(int count) { HashSet<int> randomNumbers = new HashSet<int>(); for (int i = 0; i < count; i++) while (!randomNumbers.Add(random.Next())); return randomNumbers.ToList(); } 

Note that I changed the return value from List<int> to IEnumerable<int>, if you don't plan to add/remove values from your list, you should return IEnumerable<int>, if you plan to do it, return ICollection<int>. You shouldn't return a List<int> because it is an implementation detail and that the List<> isn't made to be extensible.

Instead of using a List<int>, you should use an HashSet<int>. The HashSet<> prohibites multiple identical values. And the Add method returns a bool that indicates if the element was added to the list, this way you could change this code :

public static List<int> GetRandomNumbers(int count) { List<int> randomNumbers = new List<int>(); for (int i=0; i<count; i++) { int number; do number = random.Next(); while (randomNumbers.Contains(number)); randomNumbers.Add(number); } return randomNumbers; } 

to this :

public static IEnumerable<int> GetRandomNumbers(int count) { HashSet<int> randomNumbers = new HashSet<int>(); for (int i = 0; i < count; i++) while (!randomNumbers.Add(random.Next())); return randomNumbers.ToList(); } 

Note that I changed the return value from List<int> to IEnumerable<int>, if you don't plan to add/remove values from your list, you should return IEnumerable<int>, if you plan to do it, return ICollection<int>. You shouldn't return a List<int> because it is an implementation detail and that the List<> isn't made to be extensible.

Source Link
IEatBagels
  • 12.7k
  • 3
  • 48
  • 99

The @Jeroen's answer is the one you should take, but I wanted to review something in your code anyway, in order to make you learn something!

Instead of using a List<int>, you should use an HashSet<int>. The HashSet<> prohibites multiple identical values. And the Add method returns a bool that indicates if the element was added to the list, this way you could change this code :

public static List<int> GetRandomNumbers(int count) { List<int> randomNumbers = new List<int>(); for (int i=0; i<count; i++) { int number; do number = random.Next(); while (randomNumbers.Contains(number)); randomNumbers.Add(number); } return randomNumbers; } 

to this :

public static IEnumerable<int> GetRandomNumbers(int count) { HashSet<int> randomNumbers = new HashSet<int>(); for (int i = 0; i < count; i++) while (!randomNumbers.Add(random.Next())); return randomNumbers.ToList(); } 

Note that I changed the return value from List<int> to IEnumerable<int>, if you don't plan to add/remove values from your list, you should return IEnumerable<int>, if you plan to do it, return ICollection<int>. You shouldn't return a List<int> because it is an implementation detail and that the List<> isn't made to be extensible.