I'm attempting to fill an array of size 200 with random numbers between 1 and 1000 but I can have no repeats, and the way I'm currently doing it gives me an infinite loop, I'm assuming because it detects repeats so much. is there a better way than this, or an algorithm you could recommend, that will accomplish this better?
The first check will always be numbers[0]==numbers[0] and will always break the loop, returning to the initialization of numbers[0] which will (again) equal numbers[0], and so on. Ahhhhh.
So, why would a while loop correct that?
Edit: Would it be better to compare to y-1?
> fill an array of size 200 with random numbers between 1 and 1000 but I can have no repeats
One way to do this in linear time is:
a. create a sequence of 1000 numbers [1,2,3,...,1000].
b. shuffle the sequence randomly.
c. pick up the first 200 numbers.
random_shuffle takes two iterators, one pointing to the beginning of your array and one pointing to the end (pointers to elements of an array count as iterators), and optionally a function that can be used to generate random numbers to determine which elements should be shuffled.