Algorithm ɗin Bincike na Random, wanda kuma aka sani da binciken Monte Carlo, hanya ce ta nema bisa bazuwar. Maimakon bincika kowane nau'i a jere a cikin jerin bayanai, wannan algorithm ɗin yana zaɓar abubuwa da yawa don bincika ba da gangan ba. Wannan hanya tana adana lokaci da albarkatu idan aka kwatanta da bincike na jeri.
Yadda Ake Aiki
-
Mataki 1: Fara da tsarin bayanan da kuke son bincika.
-
Mataki na 2: Zaɓi takamaiman adadin abubuwan da za a bincika.
-
Mataki 3: Bincika abubuwan da aka zaɓa don ganin ko sun dace da yanayin bincike.
-
Mataki na 4: Idan an sami abin da ya dace, mayar da sakamakon; idan ba haka ba, koma mataki na 2.
-
Mataki na 5: Ci gaba da aiwatarwa har sai an sami wasa ko an kai matsakaicin adadin ƙoƙarin.
Abũbuwan amfãni da rashin amfani
Amfani:
- Mahimmancin Albarkatu: Yana adana lokaci da ƙwaƙwalwar ajiya, musamman don manyan bayanai.
- Randomness: Ba a iya faɗi cikin sauƙi ba, dace da yanayin da ke buƙatar bazuwar.
Rashin hasara:
- Babu Garanti na Nasara: Babu tabbacin cewa algorithm zai sami sakamakon da ake so.
- Zai iya ɗaukar lokaci mai tsawo: A cikin mafi munin yanayi, algorithm na iya ɗaukar tsayi fiye da bincike na jeri.
Misali da Bayani
Yi la'akari da misali mai zuwa na yin amfani da Algorithm na Binciken Random don nemo lamba a cikin tsararru:
import java.util.Random;
public class RandomSearchExample {
static int randomSearch(int[] arr, int target) {
Random rand = new Random();
int maxAttempts = arr.length; // Maximum number of attempts
for(int i = 0; i < maxAttempts; i++) {
int randomIndex = rand.nextInt(arr.length); // Randomly select an index
if(arr[randomIndex] == target) {
return randomIndex; // Return the index if found
}
}
return -1; // Return -1 if not found
}
public static void main(String[] args) {
int[] numbers = {1, 5, 9, 3, 7};
int target = 3;
int result = randomSearch(numbers, target);
if(result != -1) {
System.out.println("Number " + target + " found at index " + result);
} else {
System.out.println("Number " + target + " not found in the array.");
}
}
}
A cikin wannan misali, muna amfani da Algorithm na Binciken Random don nemo lamba a cikin tsararru. Muna maimaitawa ta cikin jeri, da zaɓin fihirisa ba da gangan ba, sannan mu bincika idan abin da ke wannan fihirisar ya yi daidai da lambar manufa. Idan an samo, za mu mayar da index; idan ba haka ba, za mu ci gaba har sai an kai matsakaicin adadin ƙoƙarin.