making a random distribution algorithm

Hi guys,

I am developing a mancala game where at the start of the game a player distributes some 16 seeds into their pit 1-7.

  7	  6	  5

1 2 3 4

can anyone tell me how i can distribute the seeds randomly such that pit 5-7 are never empty.

Thanks,
ngash2

I think you can make something like this (pseudocode):

Random random = new Random();

while (seeds<16) {
pit[random.nextInt(7)]++;
seeds++
}

[quote]…such that pit 5-7 are never empty.
[/quote]
Hm… that’s an odd requirement.


int pitCount = 7;
int seedCount = 16;
int mandatoryPits = 3;

int[] pits = new int[pitCount];
Random r = new Random(...);

// put 16-3=13 seeds in 7 pits
for(int i=0; i<seedCount - mandatoryPits; i++) {
   pits[r.nextInt(pitCount)]++;
}

// for the last 3 seeds, ensure that pit 5,6,7 are not empty
// if already not empty, just put the seed in a random pit
for(int p=pitCount-mandatoryPits; p<pitCount; p++) {
   if(pits[p] == 0) {
      pits[p] = 1;
   }
   else {
      pits[r.nextInt(pitCount)]++;
   }
}

// this is more 'random' than pre-filling pit 5,6,7 with seeds,
// and then randomly distributing the remaining 13 seeds

:persecutioncomplex:

Thank you for your replies , i really appreciate your help.


int used = 0;
pit[5] = random(); used += pit[5];
pit[6] = random(); used += pit[6];
pit[7] = random(); used += pit[7];
int available = 16 - used;
//distribute available seeds to other pits

At least test your code does what it is supposed to do :clue:

Sorry I didn’t really compile it or what but simply pseudo-code. But that was my solution for (what I understand from) the problem.