Problem with odds and even simulation code

Hello. I'm trying to simulate an even and odds game that scores 100 games. The problem is that when I get the results Player B seems to always win. Can anybody see whats wrong?
ScoreA=0;
ScoreB=0;
for z= 2:100
PlayerA= randi(2,[100,1]);
PlayerB= randi(2,[100,1]);
n= PlayerA+PlayerB;
if n==3
ScoreA(z)=ScoreA(z-1)+1;
ScoreB(z)=ScoreB(z-1);
else
ScoreB(z)=ScoreB(z-1)+1;
ScoreA(z)=ScoreA(z-1);
end
end

1 comentario

Erivelton Gualter
Erivelton Gualter el 22 de Oct. de 2018
I did not understand very well what you are looking forward. However, I just note your if statement is weird.
For example, for PlayerA = [2 2 1 1 1] and PlayerB = [2 1 1 2 1]; We have ScoreA = [0 0 0 0 0] and ScoreB = [0 1 2 3 4];

Iniciar sesión para comentar.

 Respuesta aceptada

Akira Agata
Akira Agata el 24 de Oct. de 2018
In your code, the variable n becomes always 100x1 array. This is the reason why the ScoreB always much higher than ScoreA.
To do your task, how about the following?
PlayerA = randi(2,[100,1]);
PlayerB = randi(2,[100,1]);
PlayerA_Win = mod(PlayerA+PlayerB,2) == 1; % Odd
PlayerB_Win = mod(PlayerA+PlayerB,2) == 0; % Even
ScoreA = sum(PlayerA_Win);
ScoreB = sum(PlayerB_Win);

Más respuestas (0)

Categorías

Más información sobre Number games en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 22 de Oct. de 2018

Respondida:

el 24 de Oct. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by