How to make a Monte carlo simulation for identifying prime numbers with a non-100% probability

10 vues (au cours des 30 derniers jours)
%Scenario:
%straight line of squares, 1-250. you start at random point.
%move left or right (50 50 chance).
%If on square 1, only move to 2. If on 250, only move to 249.
%Look at NEW number, and bleat or not.
%If on prime number, 'bleat' 85% of the time.
%If not prime, 'bleat' 30% of the time
%Find PROBABILITY that in a 15 move sequence, 'bleat' 4<x<9 times in a row.
%Plot convergence for the probability, should be approx 19.125%.
%How do I make the bleat or not section work? How do I loop this enough
%times to show convergence? Is this supposed to use a monte carlo
%simulation?
%% Making the first move.
Bleat=0;
for start==1:250
for start=randi(250,1,1) %for a random starting tile
G=rand;
if G>0.5;
FrstMove=start+1 %moves left or right 50% chance
else
FrstMove=start-1
end
if start==1; %to ensure that 1 can only move to 2
FrstMove=2
end
if start==250; %to ensure that 250 can only move to 249
FrstMove=249
end
%% Bleat or not
if FrstMove==2 || 3 || 5 || 7 || 11 || 13 || 17 || 19 || 23 || 29 || 31 || 37 || 41 || 43 || 47 || 53 || 59 || 61 || 67 || 71 || 73 || 79 || 83 || 89 || 97 || 101 || 103 || 107 || 109 || 113 || 127 || 131 || 137 || 139 || 149 || 151 || 157 || 163 || 167 || 173 || 179 || 181 || 191 || 193 || 197 || 199 || 211 || 223 || 227 || 233 || 239 || 241 && G<=0.85;; %prime number (?)
Bleat=Bleat+1 %should be if it is prime (in the list) and G<=0.85 it bleats. Does not run correctly.
elseif FrstMove~=2 || 3 || 5 || 7 || 11 || 13 || 17 || 19 || 23 || 29 || 31 || 37 || 41 || 43 || 47 || 53 || 59 || 61 || 67 || 71 || 73 || 79 || 83 || 89 || 97 || 101 || 103 || 107 || 109 || 113 || 127 || 131 || 137 || 139 || 149 || 151 || 157 || 163 || 167 || 173 || 179 || 181 || 191 || 193 || 197 || 199 || 211 || 223 || 227 || 233 || 239 || 241 && G<=0.3; %is not prime
Bleat=Bleat+1 %bleats 30% of the time if NOT prime. This elseif does not run correctly.
end
disp(Bleat) %display bleat in sequential order in the 15 loop sequence.
end
%% final probability
True=0
if bleat= %how to write bleat 4<x<9 times in a row?
True=True+1
Prob=True/ %total number of trials
end
  2 commentaires
Geoff Hayes
Geoff Hayes le 8 Juil 2021
Petch - do you have a question or are you posting code that satisfies the satement "Monte carlo simulation for identifying prime numbers with a non-100% probability"?
Petch Anuwutthinawin
Petch Anuwutthinawin le 8 Juil 2021
It is a question. I do not know why the written code does not work and I want help to fulfill the criteria of the problem.

Connectez-vous pour commenter.

Réponse acceptée

Steven Lord
Steven Lord le 8 Juil 2021
This section of code (shortened quite a bit) doesn't do what you think it does.
% if FrstMove==2 || 3
You expected this to be true if either FrstMove was equal to 2 or FrstMove was equal to 3. Let's see what it does when FrstMove was equal to 5.
FrstMove = 5;
if FrstMove==2 || 3
disp("Condition satisfied")
else
disp("Condition NOT satisfied")
end
Condition satisfied
That's because your condition is equivalent to:
% if (FrstMove==2) || 3
In the example above (FrstMove == 2) is false (since FrstMove is 5) but 3 is true (since it's not 0.) So false || true is true.
What you likely want to use is ismember.
FrstMove = 5;
if ismember(FrstMove, [2 3])
disp("Condition satisfied")
else
disp("Condition NOT satisfied")
end
Condition NOT satisfied
Of course since this isn't a primality testing code, you could just use isprime.
FrstMove = 5;
if isprime(FrstMove)
disp("Condition satisfied")
else
disp("Condition NOT satisfied")
end
Condition satisfied

Plus de réponses (1)

Walter Roberson
Walter Roberson le 8 Juil 2021
if FrstMove==2 || 3 || 5 || 7 || 11 || 13 || 17 || 19 || 23 || 29 || 31 || 37 || 41 || 43 || 47 || 53 || 59 || 61 || 67 || 71 || 73 || 79 || 83 || 89 || 97 || 101 || 103 || 107 || 109 || 113 || 127 || 131 || 137 || 139 || 149 || 151 || 157 || 163 || 167 || 173 || 179 || 181 || 191 || 193 || 197 || 199 || 211 || 223 || 227 || 233 || 239 || 241 && G<=0.85;; %prime number (?)
MATLAB does not have any numeric distributive comparison operators. You have to specify every test individually. Your code first compares a variable to 2, giving back 0 (false) or 1 (true). It then encounters the double | which is short-circuit "or". If the comparison to 2 was true then the if succeeds without evaluating the rest. If the comparison to 2 failed then it evalutes the 3 as a logical value. Values are considered true if they are nonzero and since 3 is nonzero it is considered true, and the double | succeeds.
The code does not compare the variable to 2 and then 3 and then 5 and so on. If you want to do that then I suggest using ismember()

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by