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).
Are there any other (better) approaches I missed?
Aucun commentaire:
Enregistrer un commentaire