Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

My thinking would be to bias piece selection based on a complete history of previous selection, i.e.:

    const pieces = ['I', 'J', 'L', 'O', 'S', 'T', 'Z'];
    byte[] probs = [32, 32, 32, 32, 32, 32, 32];

    piece getRandPiece(){
      int v = Math.random(pieces.length * 32)
      int sum = 0;
      int r = 0;
      for(int x = 0; x < pieces.length; x++){
        sum += probs[x];
        // check if random value with range
        if(v < sum){
          probs[x] -= pieces.length + 1;
          r = x;
        }
        // increase the probability of each piece being selected
        ++probs[x];
      }
      return pieces[r];
    }
I think the benefits would be:

* Unlikely flood and drought

* Completes in a predictable time (will always loop for each piece)

* One random number requested (can be surprisingly expensive if on a slow processor or excessive numbers of calls are made)

Are there any negatives to this approach?



view as:

> Are there any negatives to this approach?

Well, probs[x] can go negative when you decrease it, if I'm not mistaken.


Ah yes, good catch :)

Legal | privacy