Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

command rand and functions

Asked by Charlene

Charlene (view profile)

on 9 May 2013

Hi guys I have this question as an assignment question can someone help out please thanks :)

Suppose you are at a casino playing roulette and that you are betting on black to turn up. You start by betting $1. If you win you bet another $1 in the next game. If you lose double your previous bet. You will stop playing when you have either won at least 10 or lost more than $100. By use of script file simulate this system.Then play the game 5000 times and record your performance.

Hint: use the command rand

3 Comments

James Tursa

James Tursa (view profile)

on 9 May 2013

What code have you written or outlined so far?

Randy Souza

Randy Souza (view profile)

on 24 May 2013

I have restored the original text of this question.

@Charlene: this question has an accepted answer, so it may be valuable to someone else in the future. If you have a good reason why it should be removed from MATLAB Answers, please flag the question, explain why it should be deleted, and an administrator or high-reputation contributor will consider deleting the question. Please do not simply edit your question away.

Charlene

Charlene (view profile)

Products

No products are associated with this question.

1 Answer

Answer by Youssef Khmou

Youssef Khmou (view profile)

on 10 May 2013
Edited by Youssef Khmou

Youssef Khmou (view profile)

on 10 May 2013
Accepted answer

hi, try and verify this version:

N=5000;
S=zeros(1,N);
S(1)=1; % 1$
 win=0;
 lost=0;
 threshold=0.5;
 for n=1:N-1
r=rand();
if r>=threshold
    win=win+1;
    S(n+1)=1+S(n);
elseif r<threshold
    lost=lost+1;
    S(n+1)=S(n)-2;
end
if win>10
 disp('  Congra!  you won the game');
break;
end
if lost>50
disp(' You lost');
break;
end
end
figure, plot(S(1:n)), title(' Game progress')

1 Comment

José-Luis

José-Luis (view profile)

on 24 May 2013

Please do not post complete answers to homework questions.

Youssef  Khmou

Youssef Khmou (view profile)

Contact us