Jump to content

odd function problem .. skipping entries


severndigital

Recommended Posts

i have this recursive function within a class

 

protected function checkArray()
{
	$item = rand(0,parent::makeGrid());
		if(in_array($item,$this->_locations)){
			$this->checkArray();
		}else{
			return $item;
		}
}
//function that reference the above function
public function hideTreasure(){
	//echo $this->_totTreas;
	for($i = 0; $i < $this->_totTreas; $i++)
	{
	   $this->_locations[] = $this->checkArray();

	}
	return $this->_locations;
}

i designed it as a recusive function that basically it generates a random number within a rand and checks it against an array.

 

when i run this function it works fine, however if ask it to generate more than a few random numbers it starts to generate blanks.

 

here is the array from 25 randoms

 

Array
(
    [0] => 95
    [1] => 22
    [2] => 16
    [3] => 9
    [4] => 5
    [5] => 51
    [6] => 82
    [7] => 53
    [8] => 58
    [9] => 72
    [10] => 14
    [11] => 68
    [12] => 83
    [13] => 74
    [14] => 1
    [15] => 57
    [16] => 44
    [17] => 90
    [18] => 39
    [19] => 79
    [20] => 54
    [21] => 6
    [22] => 36
    [23] => 
    [24] => 45
)

 

Any ideas why it would be doing that??

 

Thanks,

C

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/126999-odd-function-problem-skipping-entries/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.