- Home /
Question by
Algebrapixel · May 11, 2020 at 12:25 PM ·
randomalgorithm
How to randomize a number without duplication?
hello,i wanted to randomize a number x amount of time without getting a duplication
is there a way to do this without taxing the system too much?
Comment
Best Answer
Answer by ShadyProductions · May 11, 2020 at 12:30 PM
You can use a hashset as this does it automatically.
int amountOfRandoms = 20;
HashSet<int> numbers = new HashSet<int>();
while (numbers.Count < amountOfRandoms ) {
numbers.Add(random.Next(1, 49));
}