mardi 10 janvier 2017

Randomly select index of matrix exactly once C++

I have a matrix of 1's (just a 2D vector of ints) and I am trying to randomly select an index so that I can turn that 1 to a 0. My goal is to select every index of the matrix exactly once so that after running a for loop with exactly as many iterations as their are indices, the matrix will be filled with 0s (the 0 doesn't actually matter, the number that replaces the 1 and the 1 itself are arbitrary).

My current approach, is very slow. It has been to run a while loop that checks each pass to see if there are still any 1's left. This is obviously incredibly inefficient, but I am not sure how to do this only once for each index and ensure there are no repeats so that I can change to a for loop. Any advice would be very helpful!




Aucun commentaire:

Enregistrer un commentaire