Hello out there!
Besser:
var wuerfel = Math.floor(Math.random() * 6);
array[wuerfel] = '';Damit bekommst du ja ne 0.
Ja, natürlich. Die hattest du mit Math.ceil(Math.random() * 6) - 1 auch bekommen. Ist ja auch der Index; und in array[0] steht was? '1'.
Der Unterschied ist: Bei meiner Variante
0 ≤ Math.random() < 1
0 ≤ Math.random() * 6 < 6
0 ≤ Math.floor(Math.random() * 6) ≤ 5
'1' ≤ array[Math.floor(Math.random() * 6)] ≤ '6'
bei deiner hingegen
0 ≤ Math.random() < 1
0 ≤ Math.random() * 6 < 6
0 ≤ Math.ceil(Math.random() * 6) ≤ 6
-1 ≤ Math.ceil(Math.random() * 6) - 1 ≤ 5
Laufzeitfehler bei Zugriff auf array[-1]
Damit bekommst du ja ne 0. Und das ist sehr wahrscheinlich (0..0,499999).
Nein, das wäre der Fall bei Math.round. Nicht aber bei Math.floor, was genau wie Math.ceil eine Gleichverteilung ergibt; nur halt keinen (unwahrscheinlichen, aber möglichen) Laufzeitfehler.
See ya up the road,
Gunnar
“Remember, in the end, nobody wins unless everybody wins.” (Bruce Springsteen)