Algorithm na Bincike bazuwar (Random Search) a cikin PHP: An Bayyana tare da Misali

Algorithm na Binciken Random wata hanya ce mai mahimmanci a cikin shirye-shiryen PHP, ana amfani da ita don bincika sararin samaniya ta zaɓin mafita da ƙima da ƙima. Manufar wannan algorithm shine don nemo mafita mai yuwuwa a cikin sararin binciken.

Yadda Algorithm na Binciken Random ke Aiki

Algorithm na Binciken Random yana farawa ta hanyar zabar saitin mafita daga sararin binciken ba da gangan ba. Sannan yana kimanta ingancin mafita ta amfani da aikin kimantawa. Algorithm na iya maimaita wannan tsari sau da yawa don nemo mafita mafi inganci.

Abũbuwan amfãni da rashin amfani Algorithm Search Random

Amfani:

  • Faɗin Bincike: Wannan Algorithm yana da damar bincika fa'idodin sararin bincike ta hanyar kimanta mafita daban-daban.
  • Sauƙi don Aiwatarwa: Algorithm na Binciken Random gabaɗaya yana da sauƙin aiwatarwa kuma baya buƙatar ƙwarewa mai yawa.

Rashin hasara:

  • Rashin Garanti na Haɓakawa na Duniya: Wannan algorithm maiyuwa ba zai sami mafita mafi kyau a duniya ba kuma yana mai da hankali kan mafita waɗanda ke kusa da matsayi na farko.
  • Cin Lokaci: Algorithm na Binciken Random na iya ɗaukar lokaci kamar yadda yake buƙatar kimanta mafita da yawa.

Misali da Bayani

Yi la'akari da misali na neman manyan lambobi a cikin takamaiman kewayon ta amfani da Algorithm Bincike na Random a cikin PHP.

function randomSearch($min, $max, $numTrials) {  
    for($i = 0; $i < $numTrials; $i++) {  
        $randomNumber = rand($min, $max);  
        if(isPrime($randomNumber)) {  
            return $randomNumber;  
        }  
    }  
    return "No prime found in the given range.";  
}  
  
function isPrime($num) {  
    if($num <= 1) {  
        return false;  
    }  
    for($i = 2; $i <= sqrt($num); $i++) {  
        if($num % $i === 0) {  
            return false;  
        }  
    }  
    return true;  
}  
  
$min = 100;  
$max = 1000;  
$numTrials = 50;  
  
$primeNumber = randomSearch($min, $max, $numTrials);  
echo "Random prime number found: $primeNumber";  

A cikin wannan misali, muna amfani da Algorithm na Binciken Random don nemo babban lamba a cikin kewayon daga 100 zuwa 1000. Algorithm ɗin yana zaɓar lambobi ba da gangan ba daga wannan kewayon kuma yana bincika idan sun kasance firamare ta amfani da aikin isPrime. Sakamakon shine babban lambar da aka samo bazuwar cikin keɓaɓɓen kewayon.

Duk da yake wannan misalin yana nuna yadda za a iya amfani da Algorithm na Binciken Random don bincika sararin bincike mai faɗi, ana iya amfani da shi ga wasu matsalolin ingantawa a cikin PHP.