For MATLAB I am trying to make a code for Russian Roullette where the player can choose how many bullets (Maximum of 6) they want to load increasing and decreasing the chances of being eliminated each spin.
This is the code I have so far. I need to have multiple scenarios where the probability is decreased with each extra bullet loaded into the chamber up until 6 which by that point, the chance of being eliminated is 100%
A=input('select chamber to load bullet, 1-6 ')
C=[A];
if C<0 || C>6;
fprintf ('I said a number between 1 to six ')
B=0;
else if C>0 && C<=6
B = round(rand()*6.0);
end
end
if A==B
disp('you are dead')
else
disp('you are alive')
B=0
end
As far as I can see,
What you have tried to do is ask the user for a random number between 1-6 telling how many bullets are there and then generation of the 'B' a random number from computer to know the location.
But if your objective is to find the probability of the elimination you don't need to go for random numbers but use a simple probability based equation.
Here it's equally likely that if there are two bullets it can in any six so a 2/6 chances that a bullet is in that one shot that is going to happen right now.
Same when all 6 bullets are there that chances of a shot being triggered when you shoot is 6/6 i.e 1 so certain elimination.
Coming to the program
A=input('select chamber to load bullet, 1-6 ')
C=[A];
if C<0 || C>6;
fprintf ('I said a number between 1 to six ')
B=0; *** don't need this variable
elseif C>0 && C<=6
Till this point you have made sure you'll get the right input,
Now based on the value of A we can make the probability as A/6 and then we can print
Use disp((A/6)*100) to return the value
Now you can just return the value of A/6*100 to tell the percentage chance you are dead or (1-A/6)*100 to know the percentage chance that you will survive.
Now since it wasn't clear what exactly you needed, I gave the more obvious answer but if you want to simulate the situation then for that you will need to do it like this
Counter = Input from user
For c = 1:counter
In a array of 0's add 1 at random value generated by Rand
If the position generated is 0 make it 1
Else retry to generate a new number between (1,6) else make the next 0 as 1
Once you have basically got a array of 0&1 where 0 represent empty and 1 represent bullet then
You can have have the rand pick one position to start, and then you can let say get 3, so you check 3rd bullet if it's 1 then you are dead to shoot, if it's 0 you are safe
For the next round check 4 and then for next 5 and then 6 after 6 you have to go back to one so for that part keep a variable increasing but take mod6+1 so that your values keep rotation between 1 to 6.
But this is a real scenario solution, for the probability based solutions you can easily do the initial part no need for loop and other parts just display the A/6*100 and get the probability
Get Answers For Free
Most questions answered within 1 hours.