Counting the number 6s rolled from a dice
29 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Nathan
am 16 Okt. 2025 um 12:53
Bearbeitet: Torsten
am 16 Okt. 2025 um 19:37
I was trying to get my code to count the number of 6s rolled from 1000 attempts from a dice however it keeps saying it has not rolled a single 6.
X= floor(rand(1,1000)*6)+1;
if X==6
y = y + 1
end
y
>> Week3Q1
y =
0
Why does it display 0 even though a 6 must have been rolled? Help would be appreciated.
0 Kommentare
Akzeptierte Antwort
Dyuman Joshi
am 16 Okt. 2025 um 13:01
Bearbeitet: Dyuman Joshi
am 16 Okt. 2025 um 13:03
X = randi([1 6], 1, 1000);
The "if" statement is only executed when all of the inputs are true -
if [true false]
disp('Hey')
elseif [true true true]
disp('Hello')
end
What you want to do is to go through each value in X and compare it to 6 -
y = 0;
for k=1:numel(X)
if X(k)==6
y = y+1;
end
end
y
You can also do it directly like this -
Y = sum(X==6)
%or
Y = nnz(X==6)
3 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!