Weighted Random Card

Suppose I have a large 2D array of values ​​in the range [0,1], where 0 means "impossible" and 1 means "very probable."

How to choose a random set of points in this array in accordance with the probabilities described above?

+2
source share
2 answers

One way to look at the problem is to ignore (for now) the fact that you are dealing with a 2d grid. You have a set of weighted items. The standard way to randomly select from this set:

  • add weights, name the amount s
  • choose a single random value 0 <= u < s
  • iterate over the items while keeping the total number of tweights of the items you examined.
  • t >= u, , (, ).

, , :

  • s ( , - , , , ).

  • 2, 3 , .

( , ), . , , - , , Google , .

EDIT: , 2D-. , ( MIPMAP) , .

+3

:

  count = 0
  maxPointsInSet = 100
  foreach(point in array){
      if(randomWithChacnce(point.value))) {
         addToSet(point)
         count++
      }
      if(count == maxPointsInSet)
         break;
  }

  function randomWithChacnce(int num){
    random = a randomized number between 0 to 1 // or random from 1 to 100 num*100
    if(num >= random)
     return true;
    return false
  }

,

0

All Articles