My question is similar to this one.
I have two lists: X with n elements and Y with m elements - let's say they hold row and column indices for a n x m matrix A. Now, I'd like to write something to k random places in matrix A.
I thought of two solutions:
- Get a random element
xfromXand a random elementyfromY. Check if something is already written toA[x][y]and if not - write. But ifkis close tom*nI can shoot like this for ever. - Create an
m*narray with all possible combinations of indices, shuffle it, draw firstkelements and write there. But the problem I see here is that if bothnandmare very big, the newly createdn*marray may be huge (and shuffling may take some time too). - Karoly Horvath suggested to combine the two. I guess I'd have to pick threshold
tand:
.
if( k/(m*n) > t ){
use option 2.
}else{
use option 1.
}
Any advice on how to pick t then?
Are there any other (better) approaches I missed?