How would I place n random elements in a two-dimensional array?
I understand how to place random numbers in an array. Thats easy iterate through it and place a random int in some range.
But... this is a set integer randomly at n different points. What is confusing me about this is that I can create some random range so up to 2 so if it's 0 or 1 that is place or don't place respectively and pseudo-random. But.. since I need exactly n different placements if I'm nearing the end of the array and these haven't been placed then they should be placed to fill the requirement of n such placements. Hence, nearing end and this isn't fulfilled would mean a higher probability of placement.
This is what is confusing me. I'm not sure how an algorithm for this would be structured to take this into account and attempt to have the placement uniform.
No modules I know I can use numpy for this
One attempt: Generate a range of the n*n grid. So a range of n^2 and pick n random numbers within this for the random placements. Then, place these in the array with accordance of the row staggering / multiples of n. Feels like a bad approach.
Aucun commentaire:
Enregistrer un commentaire