I know the basic uses of the rand() function, but I was wondering if there were more advanced uses for it. I ask this because I am making a program where a number from 0-60 is generated repetitively but I'd like it to not generate the same number twice.
So how would you exclude a number from this rand() function example:
The first solution that comes in my mind is to populate a vector of int with all the number generated and for each generation check in to the vector. This solution scale very bad though.
I thought of that solution, but the problem is that if you get down to the point where there are 2 numbers left, it will keep searching over and over until it hits one of those 2 numbers and that will take up a lot of unnecessary processing and possible a crash.
Another way is to store all possible values 1-60 in an array or vector and then shuffle them so that the order becomes random. Then you can just pick the values one after another in the order they appear.
Another solution is to have a vector of int, and use the result of the rand % the length of vector as index. Then you have only to delete the item from the vector and update the length. How about this?