Giorgio
Giorgio

Reputation: 1613

Generate random numbers with some conditions

I want to hypotize a tournament that originally starts with 9 players with 1500 chips each, then when a player exits the tournament his chips are given to the one that win against him. Mainly there are double ups/triple ups etc, but there are some other minor exchanges of chips between the players.

How can I generate randomly their chips taking into account that behaviour of chips exchange?

I would just use rand() but that wouldn't work as expected.


For example: if I have 9 players I don't have any double ups. So the stacks are similar with some variations like:

$stacks = array(1300,1150,1650,1800,1500,1550,1450,1800,1500,1550);

In a scenario with 4 players there would have been 5 double ups:

$stacks = array(7500,1500,1500,1500) or array(4500,3000,3000,1500); //this should be randomized a little bit to

$stacks = array(7800,1350,1250,1950);

I hope I've been clear, if not I will edit back the post!

Upvotes: 0

Views: 329

Answers (1)

Mikhail
Mikhail

Reputation: 9007

If I understand your concern correctly, here's one way to do this:

for ($x = 0; $x < $NUMBER_OF_EXCHANGES; $x++) {
  $loser = array_rand($stacks);
  $winner = array_rand($stacks);
  $stacks[$loser] -= 50;
  $stacks[$winner] += 50;
}

Upvotes: 1

Related Questions