Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fill 2 dimensional array random in a bingo way

Tags:

php

I've got these two functions:

function drawNumber($drawnNumbers){
  $unique = true;

  while ($unique == true){
      $number = mt_rand(10, 69);
      if (!in_array($number, $drawnNumbers)){
        return $number;
        $unique = false;
      }
  }
}

 fillCard();  ?>

It's a bingo game. The card gets filled with random Numbers. But I can't get it like this:

column column column column column column
row 10     13     16     14     16     19
row 24     26     28     29     23     21
row 36     33     39     30     31     35
row 46     48     42     45     43     47
row 59     56     51     52     58     50
row 60     65     68     62     61     67

So I would like to have the first row with numbers from 10 to 19 the second row from 20 to 29 and so on.

I tried like this

<?php drawnNumber():    $number = mt_rand(0,9);
fillArray():  $number = $row . $number; ?>

But that doesn't work, because there are double numbers in the card.

So before that I tried it in a different way,with in_array:

<?php 
function fillCard(){
      $card = array();

    /* fill card with random numbers */
    for($i = 0, $min = 10, $max = 19; $i < 6; $i++, $min+=10, $max += 10)
    {
        for($j = 0; $j < 6; $j++)
        {
        $number =  mt_rand($min,$max) ;
    if(!in_array($number, $card){
            $card['row' . $i]['column' . $j]['number'] = $number;
            $card['row' . $i]['column' . $j]['found'] = 0;
        }
        }
    }
var_dump($card);
return $card;
}  ?>

But there are still double random numbers in the card. I tried a few other thinks in the last two weeks, but I just can't get it to work together. If one thing succeeds the other thing fails.

I can get the random numbers but not unique random numbers in the card. I hope someone can help me. (for extra information: it's a bingo game. So drawnNumber() are the "balls", which get drawn and stored in the array $drawnNumbers, they also are unique random numbers. fillCard() is the function that fills the bingo card and checks if $drawNumber is in $card)

I would appreciate some help, if someone can tell me how to get it to work. Maybe in an algorithm way or else some code? Thank you in advance.

like image 982
Rick Vasilev Avatar asked Aug 20 '13 11:08

Rick Vasilev


Video Answer


1 Answers

In general, you draw from some kind of box, right? So do the same, have an array with all available numbers and once you get a random number out of it, remove it, so the next time you search for a number, you will only pick from the remaining ones. Small example:

a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4

we pick a random number between 0 and 3 inclusive (0 and the length - 1 of a that is). Let's say we picked index 2, then a will look like:

a[0] = 1
a[1] = 2
a[2] = 4

Now if you draw a number between 0 and 2 (note that you take the length - 1 of a!), you won't re-pick the already chosen number in any way thus giving you unique numbers ALL the time.

P.S. this is a simplified version, but now if you can apply that to yourself and, for your example, create several arrays you will pick from.

like image 170
Andrius Naruševičius Avatar answered Sep 19 '22 00:09

Andrius Naruševičius