How to make a Monte carlo simulation for identifying prime numbers with a non-100% probability
Show older comments
%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 Comments
Geoff Hayes
on 8 Jul 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
on 8 Jul 2021
Accepted Answer
More Answers (1)
Walter Roberson
on 8 Jul 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()
Categories
Find more on Chemistry in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!