The Student Room Group

Coin toss on Matlab?

Simulate a coin toss: write a code, that can have two different outputs,
each with probability
first bit was just let coin=randi(2);
(b) Write a script that carries out coin tosses, until the result is three consecutive times the same. Save the number of coin tosses.
any ideas for the next one
ik counter +1 will be needed but im not too sure
Original post by johnsonstevensom
Simulate a coin toss: write a code, that can have two different outputs,
each with probability
first bit was just let coin=randi(2);
(b) Write a script that carries out coin tosses, until the result is three consecutive times the same. Save the number of coin tosses.
any ideas for the next one
ik counter +1 will be needed but im not too sure

Counter will be needed. Code the repetition for coin tosses and add the condition. In this case the condition is 'until the result is three consecutive times the same'. In order for it to be consecutively same you will need HHH or TTT. A 'HTT, HTH, HHT, THT, TTH' will not satisfy the condition.
Original post by johnsonstevensom
Simulate a coin toss: write a code, that can have two different outputs,
each with probability
first bit was just let coin=randi(2);
(b) Write a script that carries out coin tosses, until the result is three consecutive times the same. Save the number of coin tosses.
any ideas for the next one
ik counter +1 will be needed but im not too sure

Do you understand it?
Original post by Wilfried Bony
Do you understand it?

counter =0;
while
if
counter=counter+1;
end
heads=randi(2);
tails=randi(2);
my problem is i cant fill the gaps i dont see a code which could satisfy all 3
Reply 4
Original post by johnsonstevensom
Simulate a coin toss: write a code, that can have two different outputs,
each with probability
first bit was just let coin=randi(2);
(b) Write a script that carries out coin tosses, until the result is three consecutive times the same. Save the number of coin tosses.
any ideas for the next one
ik counter +1 will be needed but im not too sure


Ive got a similar question but with rolling a die. I think doing this one will help. Can anyone help me on this question? Surely it cant take longer than 5 minutes.
just call rand(2) and put the result in an array each iteration. iterate until you see three consecutive identical numbers in the array
Reply 6
Original post by MalcolmX
just call rand(2) and put the result in an array each iteration. iterate until you see three consecutive identical numbers in the array

How would i iterate the result?
Reply 7
Original post by MalcolmX
just call rand(2) and put the result in an array each iteration. iterate until you see three consecutive identical numbers in the array

what are the conditions?
Reply 8
Original post by MalcolmX
just call rand(2) and put the result in an array each iteration. iterate until you see three consecutive identical numbers in the array

Could you show the steps please?
Original post by THenry99
Could you show the steps please?


Original post by johnsonstevensom
counter =0;
while
if
counter=counter+1;
end
heads=randi(2);
tails=randi(2);
my problem is i cant fill the gaps i dont see a code which could satisfy all 3

Try using this to start @RDKGames @MalcolmX
(edited 3 years ago)

clc
clear

TossCounter = 0 ;

TossArray = zeros(1,3) ;

% Define 1 = HEADS and 2 = TAILS

Consecutives = 0 ;

while Consecutives == 0

Toss = randi(2) ;

TossCounter = TossCounter + 1 ;

if TossCounter == 1

TossArray(1) = Toss ;

elseif TossCounter == 2

TossArray(2) = Toss ;

elseif TossCounter == 3

TossArray(3) = Toss ;

else

TossArray(1) = TossArray(2) ;
TossArray(2) = TossArray(3) ;
TossArray(3) = Toss ;

end

if TossArray == [1 1 1]

Consecutives = 1 ;

elseif TossArray == [2 2 2]

Consecutives = 1 ;

end

end

fprintf('Number of tosses = %d \n\n',TossCounter)

(edited 3 years ago)

Quick Reply

Latest