Search code examples
phpalgorithmcounter

randomizing according an counter and the counter ends with the exact same value as it started with


I'm having some trouble with a weird code structure and I can't seem to find an answer to the problem after 2 hours of try and error.

I have 2 variables and an array structure with some sub arrays (this is important for later on) the system is to big to post the full code so here is the problem part:

$counter= 10;
$times = 20;
//Random 1 / 0

while($times > 0){
    $number = rand(0,1);
    if($number == 1) {
        $counter++;
    } else {
        $counter--;
    }
$times--;
}

So this is straightforward, problem however is that the $total which is 10 at the start has to end on 10 as well, so there has to be 10 times an 1 and 10 times an 0.

I tried to work around it using:

$number  = 0;
if ($counter > 0) { 
    $number = 1; 
    $counter--;
}

Which worked and is a simple solution but see the 'addition 1`

But this has to be randomized. And each of those positions is in the actual program an array so shuffling the array isn't an option.

TLDR: I need to shuffle only 1 part of an array or have to random generate the 0 or 1 with the given code in the first code sample. And to be honest, I have no idea at this moment how I could do this so any help is appreciated.

Addition 1:

array(
      array('I' => 1, 'X' => '7'),  
      array('I' => 1, 'X' => '7'),  
      array('I' => 1, 'X' => 'value'), 
      array('I' => 1, 'X' => 'value'), 
      array('I' => 1, 'X' => 'value'),
      array('I' => 1, 'X' => 'value'),
      array('I' => 1, 'X' => 'value'),
      array('I' => 1, 'X' => 'value'),
      array('I' => 1, 'X' => 'value'),
      array('I' => 1, 'X' => 'value'),
      array('I' => 0, 'X' => 'value'),
      array('I' => 0, 'X' => 'value'),
      array('I' => 0, 'X' => 'value'),
      array('I' => 0, 'X' => 'value'),
      array('I' => 0, 'X' => 'value'),
      array('I' => 0, 'X' => 'value'),
      array('I' => 0, 'X' => 'value'),
      array('I' => 0, 'X' => 'value'),
      array('I' => 0, 'X' => 'value'),
      array('I' => 0, 'X' => 'value'),
);

each value can occur 2 times (will not always occur 2 times) and if this is the case one of the 2 value has to be 'I' => 1 and 'I' => 0 but the X is filled with a random as well:

$x = rand(1, 100)

This is the actual structure sorry if I was unclear with the initial post and now my problem is more clear I presume.

Addition 2:

The expected result:

//This is correct
'I' => 1, 'X' => 7
'I' => 0, 'X' => 7

//This is not correct:

'I' => 1, 'X' => 7
'I' => 1, 'X' => 7

//This is not correct
'I' => 0, 'X' => 7
'I' => 0, 'X' => 7
  • 10 times an 'I' => 1
  • 10 times an 'I' => 0
  • If an 'X' occurs 2 times see above code sample:

Solution

  • $yourSample = [
      0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0, 9 => 0, 
      10 => 1, 11 => 1, 12 => 1, 13 => 1, 14 => 1, 15 => 1, 16 => 1, 17 => 1, 18 => 1, 19 => 1
    ];
    
    $total = count($yourSample);
    $newIndexes = [];
    
    
    for ($i = 0; $i < $total; $i++) {
     $randItem = array_rand($yourSample);
     $newIndexes[] = $yourSample[$randItem];
     unset($yourSample[$randItem]);
    }
    
    //echo '<pre>';
    //var_dump($newIndexes);
    

    Is it fits your issue, right?

    Update 1:

    <?php
    
    function getRandomX($sample, $spe)
    {
      $rand = mt_rand(0, 100);
      foreach ($sample as $element) {
         if ($element['X'] == $rand and $spe['I'] == $element['I']) {
            $rand = getRandomX($sample, $spe);
         }
      }
    
      return $rand;
    }
    
    
    function makeResult()
    {
      $sample = array_merge(
          array_fill(0, 10, ['I' => 1, 'X' => null]),
          array_fill(0, 10, ['I' => 0, 'X' => null])
      );
      shuffle($sample);
    
      foreach ($sample as &$value) {
          $value['X'] = getRandomX($sample, $value);
      }
    
      return $sample;
    }
    
    echo '<pre>';
    var_export(makeResult());