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
x
fromX
and a random elementy
fromY
. Check if something is already written toA[x][y]
and if not - write. But ifk
is close tom*n
I can shoot like this for ever. - Create an
m*n
array with all possible combinations of indices, shuffle it, draw firstk
elements and write there. But the problem I see here is that if bothn
andm
are very big, the newly createdn*m
array 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
t
and:
.
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?