Question

For MATLAB I am trying to make a code for Russian Roullette where the player can...

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

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In the script below, where should I insert the break command so that the code produces...
In the script below, where should I insert the break command so that the code produces the following output: 10 11 12 26 a = 10; for i=a:i+10 if( a<13 ) ...(1)... disp(a); a = a+1; ...(2)... else disp(a*2); ...(3)... end ...(4)... end a. (4) b. (1) c. (3) d. (2)
Newton's method for finding the root in MATLAB. I am stuck on the two lines of...
Newton's method for finding the root in MATLAB. I am stuck on the two lines of codes which are required to complete. can anyone give me a hint on this? function sample_newton(f, xc, tol, max_iter) % sample_newton(f, xc, tol, max_iter) % finds a root of the given function f over the interval [a, b] using Newton-Raphson method % IN: % f - the target function to find a root of % function handle with two outputs, f(x), f'(x) % e.g.,...
I need to change the MATLAB code below to something else, but I need the solutions...
I need to change the MATLAB code below to something else, but I need the solutions to match each other. (Also try not to copy answer posted on chegg already) % % Problem 1.8 % clc; clear all; close all; % reset matlab w orthregdata; % load data n = length(a); w = ones(n,1); ma = w'*a/n; mb = w'*b/n; % ma = mean(a); % mb = mean(b); sa = norm(a-ma)/sqrt(n); sb = norm(b-mb)/sqrt(n); % sa = std(a,1); % Pass...
Problem 3 you can use Matlab and also i give u the Problem 1 code its...
Problem 3 you can use Matlab and also i give u the Problem 1 code its on Matlab Using the same initial code fragment as in Problem 1, add code that calculates and plays y (n)=h(n)?x (n) where h(n) is the impulse response of an IIR bandpass filter with band edge frequencies 750 Hz and 850 Hz and based on a 4th order Butterworth prototype. Name your program p3.sce this is the Problem 1 code and the solutin clear; clc;...
MATLAB CODE: Matlab’s polyder() and polyint() functions return the derivative and integral of a polynomial, respectively....
MATLAB CODE: Matlab’s polyder() and polyint() functions return the derivative and integral of a polynomial, respectively. For example, polyder([4 -2 3 7 -5]) yields [16 -6 6 7] in other words: d/dx{4x4−2x3 +3x2+7x−5} = 16x3−6x2 +6x+7and,polyint([16 -6 6 7]) yields [4 -2 3 7 0] note the zero at the end. in other words:∫(16x3-6x2+6x+7)=4x4–2x3+ 3x2+7x+? where C = 0 As seen above, the constant of integration is always assumed to be zero when using polyint().While polyint() is available, using your...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for...
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for an integer between 0 and 9999, inclusive. The script should then calculate the digit in each of the 1000’s, 100’s, 10’s, and 1’s place of the number. Create a variable for each of the 4 extracted digits. For example, if your variables are named nThousands, nHundreds, nTens, and nOnes, then, in the case of 9471, they would be end up being set to 9,...
I am trying to make a program in C# and was wondering how it could be...
I am trying to make a program in C# and was wondering how it could be done based on the given instructions. Here is the code that i have so far... namespace Conversions { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if...
I am making a game like Rock Paper Scissors called fire water stick where the rules...
I am making a game like Rock Paper Scissors called fire water stick where the rules are Stick beats Water by floating on top of it Water beats Fire by putting it out Fire beats Stick by burning it The TODOS are as follows: TODO 1: Declare the instance variables of the class. Instance variables are private variables that keep the state of the game. The recommended instance variables are: 1. 2. 3. 4. 5. 6. A variable, “rand” that...
I am using matlab and getting a "matrix dimensions error below" for line 22. Can someone...
I am using matlab and getting a "matrix dimensions error below" for line 22. Can someone spot the error and try the code to fix the error. %The beginning step is to generate a functionf(t) that consists of the sum %of the following components % 25 Hz cosine function of magnitude 1 % 50 Hz sine function of magnitude 1 % 40 Hz square wave function of magnitude 1 clear; clc; close all; %sample rate is given at 2500 Hz...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT