Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.