Why does my Button with a script not work sometimes? - javascript

I've made a simple script using Javascript which generates a random number between 0-5+1 (Dice.) My issue is that when I press the button, it does nothing half the time forcing me to spam the button multiple times before a dice roll would appear.
I've looked everywhere, but couldn't find any resources as to what the issue might be.
Here is my code:
<html>
<head>
</head>
<body>
<button type="button" onclick="spin()">Click here to roll the Dice!</button>
<img src="" id="demo">
<script>
function spin() {
if (Dice() == 1) {
document.getElementById("demo").src="http://i.imgur.com/QRTs9Ax.png";
}
else if (Dice() == 2) {
document.getElementById("demo").src="http://i.imgur.com/OMz1o8U.png";
}
else if (Dice() == 3) {
document.getElementById("demo").src="http://i.imgur.com/J4Xx2yO.png";
}
else if (Dice() == 4) {
document.getElementById("demo").src="http://i.imgur.com/CJb2ojk.png";
}
else if (Dice() == 5) {
document.getElementById("demo").src="http://i.imgur.com/8W6UL5O.png";
}
else if (Dice() == 6) {
document.getElementById("demo").src="http://i.imgur.com/NGxBete.png";
}
}
</script>
<script>
function Dice() {
return Math.floor(Math.random() * 6) + 1;
}
</script>
</body>
</html>

Because Math.floor(Math.random() * 6) + 1; could return 7.
EDIT
Math.floor(Math.random() * 6) + 1; can't return 7 according to Mozilla Developers because Math.random() doesn't include 1 and Math.floor just cut the value so you'll always recive a value between 1 and 6.
The proble is that with if (Dice() == x) you start the function so it will retur a diferent vaule for all the is statement. You have to build a switch like this:
function spin() {
switch (Dice()) {
case 1:
document.getElementById("demo").src = "http://i.imgur.com/QRTs9Ax.png";
break;
case 2:
document.getElementById("demo").src = "http://i.imgur.com/OMz1o8U.png";
break;
case 3:
document.getElementById("demo").src = "http://i.imgur.com/J4Xx2yO.png";
break;
case 4:
document.getElementById("demo").src = "http://i.imgur.com/CJb2ojk.png";
break;
case 5:
document.getElementById("demo").src = "http://i.imgur.com/8W6UL5O.png";
break;
case 6:
document.getElementById("demo").src = "http://i.imgur.com/NGxBete.png";
break;
}

Store the value of dice() once to a variable and check it with the if statements. You are regenerating the the value each time so there is no guarantee you will get the current value when checking.
Also, as mentioned by paolo, Math.floor(Math.random() * 6) + 1; could return 7.
<html>
<head>
</head>
<body>
<button type="button" onclick="spin()">Click here to roll the Dice!</button>
<img src="" id="demo">
<script>
function spin() {
var dice = Dice();
if (dice == 1) {
document.getElementById("demo").src="http://i.imgur.com/QRTs9Ax.png";
}
else if (dice == 2) {
document.getElementById("demo").src="http://i.imgur.com/OMz1o8U.png";
}
else if (dice == 3) {
document.getElementById("demo").src="http://i.imgur.com/J4Xx2yO.png";
}
else if (dice == 4) {
document.getElementById("demo").src="http://i.imgur.com/CJb2ojk.png";
}
else if (dice == 5) {
document.getElementById("demo").src="http://i.imgur.com/8W6UL5O.png";
}
else if (dice == 6) {
document.getElementById("demo").src="http://i.imgur.com/NGxBete.png";
}
}
</script>
<script>
function Dice() {
return Math.floor(Math.random() * 6) + 1;
}
</script>
</body>
</html>

You're trying to keep your random number from returning 0 by incrementing it by + 1 after flooring. However, when Math.random() returns exactly 1.0, your multiplication will result in exactly 6, which gets floored to 6 and then gets + 1 added to it, making it 7.
Instead, only multiple your random number by 5.
return Math.floor(Math.random() * 5) + 1;
Or, even more simply, just use Math.ceil(). Which will always round up to the nearest whole number.
return Math.ceil(Math.random() * 6);

You're calling the Dice() function on every else if statement.
I've created a fiddle where you can see on the console, which number is been generated, but your spin function would go like this:
function spin() {
var number = Dice();
console.log(number);
switch (number) {
case 1:
document.getElementById("demo").src="http://i.imgur.com/QRTs9Ax.png";
break;
case 2:
document.getElementById("demo").src="http://i.imgur.com/OMz1o8U.png";
break;
case 3:
document.getElementById("demo").src="http://i.imgur.com/J4Xx2yO.png";
break;
case 4:
document.getElementById("demo").src="http://i.imgur.com/CJb2ojk.png";
break;
case 5:
document.getElementById("demo").src="http://i.imgur.com/8W6UL5O.png";
break;
case 6:
document.getElementById("demo").src="http://i.imgur.com/NGxBete.png";
break;
}
}
Also you may get the same number multiple times, since it's random.

Just because Dice() hasn't static value and may return different value with each if statement, also may return the same random number with multiple if.
So store generated number in a variable and do your if statements as you like.
This below example based on selecting random image from imgs array and should do the task as your code do.
//add images what ever.
var imgs = [
"http://i.imgur.com/QRTs9Ax.png",
"http://i.imgur.com/OMz1o8U.png",
"http://i.imgur.com/J4Xx2yO.png",
"http://i.imgur.com/CJb2ojk.png",
"http://i.imgur.com/8W6UL5O.png",
"http://i.imgur.com/NGxBete.png"
];
function spin() {
var d = Dice(); //store the number in a variable
//Dice(5); ===> if you want the max random number will be 5
var i = d-1; //to get the right position in array
if(imgs[i])document.getElementById("demo").src = imgs[i];
//Also if you want additional statements use if(d == 1){}else if(d == 2){}
}
function Dice(max){
max = max || imgs.length;
return Math.floor(Math.random() * max) + 1;
}

Related

trigger code after x amount of increments

thanks for looking into this for me.
The issue I am facing is this:
I am keeping track of a variables value, i want to trigger blocks of code after certain values are reached.
currently I am using a switch statement but I am looking for something dynamic.
Example I have now:
class className{
constructor(param){
this.param = param
switch(param.x){
case 25:
param.y += 0.1;
break;
case 50:
y += 0.1;
break;
case 75:
y += 0.1;
break;
}
}
As you can see i want run a block of code after the value of X increments by 25
to manually code each block becomes tedious
i want the same code to run every time the value of x increments by 25
x has no limit and this increments infinitely
so i was looking for some kind of infinite loop or something
does anyone know what I can use for this particular situation
If i could right it like this:
param.x.forEach(25){
param.y += 0.1;
}
I did try this above but to no avail it did not work lol
how can i do this guys any help please?
You could do the calculation like this (x % 25) == 0
var y = 0;
var x = 0;
function trig() {
x++;
console.log(x)
if ((x % 25) == 0) {
y += 0.1;
console.log(y, x)
}
}
setInterval(() => {
trig()
}, 100)
ON Class
class className {
constructor(param) {
this.param = param
}
inc() {
this.param.x++;
if ((this.param.x % 25) == 0) {
this.param.y += 0.1;
}
}
//you could call the inc() function on you click or activity
}
I believe you can just check if (x % 25 === 0 ){ ..do stuff }

Basic JavaScript: Counting Cards - Card Counting Function with Strings

The card function has been explained multiple times, and I understand it. For those who do not know it: my function receives a card parameter, which can be a number or a string. I then increment or decrement the global count variable according to the card's value 2,3,4,5,6 increments, 7,8,9 keeps it at 0, while 10, J, Q, K, A decrements it. My function then returns a string with the current count and the string "Bet" if the count is positive, or "Hold" if it is negative.
So I understand how the function is done, and FreeCodeCamp accepted my solution as technically it meets their conditions. But have a question regarding this function:
var count = 0;
function cc(card) {
if (card >= 2 && card <= 6) {
count++;
} else if (card >= 7 && card <= 9) {
count += 0;
} else {
count--;
}
if (count <= 0) {
return count + " Hold";
} else {
return count + " Bet";
}
}
console.log(cc(2));
console.log(cc(3));
console.log(cc(7));
console.log(cc('K'));
console.log(cc('A'));
As I can see, the first condition is fairly simple and easy to define, so is the else if. In the third case, there are both numbers and strings involved. Does this not mean that when I put ANY string into cc, it will decrement? As anything that is not between 2 and 6, or 7 and 9, will automatically decrement? Even if the user inputs something that is not a card or is not a value from the list?
I understand that there is a list of predefined card values and names, but nevertheless, is there any better way to condition my statement to make sure that my condition will ONLY run IF the card is either 10, J, Q, K or A, and not any other value?
You can change your current else, to return and error message or just return immediately in case of the input being a non-valid card, and add another else-if to check for 10 through Ace:
if (card >= 2 && card <= 6) {
count++;
} else if (card>=7 && card <=9) {
count+= 0;
} else if (card === 10 || card === 'J' || card === 'Q' || card === 'K' || card === 'A'){
count--;
}else {
//Either just return or alert an error message and return
}
There are a number of ways you could deal with this situation. You could initially parse the input, and say assign 'J' to 11, 'Q' to 12, 'K' to 13 and 'A' to 1 (if you need to distinguish), or just a common number to that category. Everything else is an invalid input and you return immediately/post an error message. Something like:
var count = 0;
function cc(card) {
if (card == 'J' || card == 'Q' || card == 'K' || card == 'A')
card = 11;
if (card >= 2 && card <= 6) {
count++;
} else if (card>=7 && card <=9) {
count+= 0;
} else if (card >= 10 && card <= 11) {
count--; // to keep structure cleaner we use dummy 11 value
} else
//error message
if (count <= 0) {
return count + " Hold";
} else {
return count + " Bet";
}
}
cc(2); cc(3); cc(7); cc('K'); cc('A');
Also, you need to make sure to handle lower case and upper case values for the picture cards.
Define a set of allowed values and check if the value you are given is within that set using .includes(). For example:
var count = 0;
function cc(card) {
// Only change code below this line
const up = [2,3,4,5,6];
const no = [7,8,9];
const down = [10, "J", "Q", "K", "A"];
if(up.includes(card))count++;
if(down.includes(card))count--;
const str = count > 0 ? "Bet" : "Hold";
return `${count} ${str}`;
// Only change code above this line
}
// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(7); cc('K'); cc('A');
Bear in mind this is type sensitive.
Another possibility is something like the following, which explicitly lists the changes for each card:
const counter = () => {
let count = 0
let values = {2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 0, 8: 0,
9: 0, 10: -1, J: -1, Q: -1, K: -1, A: -1}
return (card) => {
const change = values[card] || 0 // no change if card is, say, 'XYZ' or 'Joker'
count += change
return count <= 0 ? 'Hold' : 'Bet'
}
}
const cc = counter();
console.log(cc(2));
console.log(cc(3));
console.log(cc(7));
console.log(cc('K'));
console.log(cc('A'));
For a list as short as thirteen values, I think this sort of explicit list is cleaner.
This also encapsulates the count variable in a closure. I find that cleaner than a global variable.
Where the comment talks about jokers, you might want some more robust error-handling:
if (!(card in values)) {throw 'Bad card'}
const change = values[card]
You could use a regular expression at the very top of your function to skip all the conditionals and return a handy message if the argument passed in doesn't match a valid card:
// Check if card is valid
var cardRegex = /^(10|[2-9AJQK])$/i;
if (!cardRegex.test(card)) {
return "Invalid Card";
}
So, in the context of your code, it would look like:
var count = 0;
function cc(card) {
// Check if card is valid
var cardRegex = /^(10|[2-9AJQK])$/i;
if (!cardRegex.test(card)) {
return "Invalid Card";
}
if (card >= 2 && card <= 6) {
count++;
} else if (card >= 7 && card <= 9) {
count += 0;
} else {
count--;
}
if (count <= 0) {
return count + " Hold";
} else {
return count + " Bet";
}
}
// Valid inputs
console.log(cc(2));
console.log(cc(3));
console.log(cc(7));
console.log(cc('K'));
console.log(cc('a'));
// Invalid inputs
console.log(cc('e'));
console.log(cc('L'));
console.log(cc(0));
My solution for Basic JavaScript: Counting Cards
function cc(card) {
// Only change code below this line
if(card >= 2 && card <= 6) {
count++;
} else if (card === 10 ||card === 'J' || card === 'Q' || card === 'K' || card === 'A') {
count = count - 1;
}
if (count > 0) {
return count + ' Bet';
}
return count + ' Hold';
// Only change code above this line
}
My solution, based on what we learned so far.
Maybe it isnĀ“t the best, but it also works.
var count = 0;
function cc(card) {
// Only change code below this line
switch(card){
case 2:
case 3:
case 4:
case 5:
case 6:
count++;
break
case 7:
case 8:
case 9:
count = count;
break
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
count--;
break;
}
if (count <=0) {
return count + ' Hold';
}
else {
return count + ' Bet'
}
// Only change code above this line
}
console.log(cc(2));
console.log(cc(3));
console.log(cc(7));
console.log(cc('K'));
console.log(cc('A'));
let count = 0;
function cc(card) {
switch (card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count++;
break;
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
count--;
}
if (count > 0) {
return count + " Bet";
} else {
return count + " Hold";
}
}

Javascript if/else statements not working

I am pretty new to Javascript, and it seems like i didnt understand the if else statements correctly.
I have a script which will make the visitor go to 1 of 4 websites, but the 2 last sites in my code does not work.
<script>
setTimeout(function() {
var r = Math.random();
if(r > 0.49) {
window.location.replace("1.html");
}
else if(r < 0.48) {
window.location.replace("2.html");
}
if (r == 0.48){
window.location.replace("maybe.html");
}
else if (r == 0.49){
window.location.replace("4.html");
}
}, 1);
</script>
Is how my code looks like right now. How would it need to look to make it work?
Update
I originally said this looked fine, but I just noticed a problem. There is no branch for r > 0.48 && r < 0.49. Values in this range, such as 0.48342... are more likely than hitting 0.48 or 0.49 exactly, and these are completely unaccounted for, which I assume was not your intention. A simple else branch is always a good idea, or you should account for these cases explicitly.
Original
Your logic looks fine to me. Reduce your problem:
function randomizeText() {
var r = Math.random();
var result = '';
if (r > 0.49) {
result = 'A';
}
else if (r < 0.48) {
result = 'B';
}
else if (r == 0.48){
result = 'C';
}
else if (r == 0.49){
result = 'D';
}
document.getElementById('output').innerText = result;
document.getElementById('random-number').innerText = 'Number was: ' + r;
}
randomizeText();
<button type="button" onclick="randomizeText();">Randomize!</button><br>
<div id="output"></div>
<div id="random-number"></div>
Note that it's going to be very very unlikely that you'll hit either of the last 2 conditions.
You can replace your entire code block with these 2 lines, and they will do what you want:
var r = Math.floor(Math.random() * 4) + 1;
window.location.replace(r+".html");
Explanation:
Your code is actually working. The problem is that the number returned by Math.random() is a random number between 0 and 1 (it might be 0.5544718541204929 ), and will almost NEVER be exactly 0.48 or 0.49, but will almost always be between those two numbers.
A better solution would be:
var r = Math.floor(Math.random() * 4) + 1;
and then test if number is 1, 2, 3 or 4.
Example:
jsFiddle Demo //jsFiddle temporarily not saving fiddles
var r = Math.floor(Math.random() * 4) + 1;
if(r ==1) {
alert("1.html");
}else if(r==2){
alert("2.html");
}else if(r==3){
alert("3.html");
}else{
alert("4.html");
}
BUT there is no need for the entire IF block. Just do this:
var r = Math.floor(Math.random() * 4) + 1;
window.location.replace(r+".html");
//alert( r + ".html" );
In response to the this question, submitted as a comment: I want it to be page 1 and page 2 has is almost 50/50, and the last 2 is pretty rare
This would give odds of 1% for cases 3 and 4.
var r = Math.floor(Math.random() * 100) + 1; //return number between 1 and 100
if(r <=48) {
alert("1.html");
}else if(r<=98){
alert("2.html");
}else if(r==99){
alert("3.html");
}else{ //r==100
alert("4.html");
}
If you desire slightly larger odds:
if(r <=40) { //40% chance
alert("1.html");
}else if(r<=80){ //40% chance
alert("2.html");
}else if(r<=90){ //10% chance
alert("3.html");
}else{ //r is between 91 and 100, 10% chance
alert("4.html");
}

javascript fizzbuzz switch statement

I'm currently taking the code academy course on Javascript and I'm stuck on the FizzBuzz task. I need to count from 1-20 and if the number is divisible by 3 print fizz, by 5 print buzz, by both print fizzbuzz, else just print the number. I was able to do it with if/ else if statements, but I wanted to try it with switch statements, and cannot get it. My console just logs the default and prints 1-20. Any suggestions?
for (var x = 0; x<=20; x++){
switch(x){
case x%3==0:
console.log("Fizz");
break;
case x%5===0:
console.log("Buzz");
break;
case x%5===0 && x%3==0:
console.log("FizzBuzz");
break;
default:
console.log(x);
break;
};
};
Switch matches the x in switch(x){ to the result of evaluating the case expressions. since all your cases will result in true /false there is no match and hence default is executed always.
now using switch for your problem is not recommended because in case of too many expressions there may be multiple true outputs thus giving us unexpected results. But if you are hell bent on it :
for (var x = 0; x <= 20; x++) {
switch (true) {
case (x % 5 === 0 && x % 3 === 0):
console.log("FizzBuzz");
break;
case x % 3 === 0:
console.log("Fizz");
break;
case x % 5 === 0:
console.log("Buzz");
break;
default:
console.log(x);
break;
}
}
I thought switch too,but no need.
for (var n = 1; n <= 100; n++) {
var output = "";
if (n % 3 == 0)
output = "Fizz";
if (n % 5 == 0)
output += "Buzz";
console.log(output || n);
}
Switch statement checks if the situation given in the cases matches the switch expression. What your code does is to compare whether x divided by 3 or 5 is equal to x which is always false and therefore the default is always executed. If you really want to use a switch statement here is one way you can do.
for (var i=1; i<=30; i++){
switch(0){
case (i % 15):
console.log("fizzbuzz");
break;
case (i % 3):
console.log("fizz");
break;
case (i % 5):
console.log("buzz");
break;
default:
console.log(i);
}
}
Not to too my own horn but this is much cleaner:
var numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
for (var i = 1; i <= numbers.length; i++) {
if (i % 15 === 0) {
console.log("FizzBuzz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} else {
console.log(i);
}
};
The switch(true) part of this statement helped me. I was trying to do a switch statement for fizzbuzz. My solution incorporates the coding style of Rosettacodes - general solution. Most significantly the use of force typing to shorten the primary conditionals. I thought, it was valuable enough to post:
var fizzBuzzSwitch = function() {
for (var i =0; i < 101; i++){
switch(true) {
case ( !(i % 3) && !(i % 5) ):
console.log('FizzBuzz');
break;
case ( !(i % 3) ):
console.log('Fizz');
break;
case ( !(i % 5) ):
console.log('Buzz');
break;
default:
console.log(i);
}
}
}
Here's what made it clear for me, might help :
It's a misinterpretation of what switch (x){} means.
It doesn't mean : "whenever whatever I put inbetween those brackets is true, when the value of x changes."
It means : "whenever x EQUALS what I put between those brackets"
So, in our case, x NEVER equals x%3===0 or any of the other cases, that doesn't even mean anything. x just equals x all the time. That's why the machine just ignores the instruction. You are not redefining x with the switch function. And what you put inbetween the brackets describes x and x only, not anything related to x.
In short :
With if/else you can describe any condition.
With switch you can only describe the different values taken by the variable x.
Here's a solution incorporating #CarLuvr88's answer and a switch on 0:
let fizzBuzz = function(min, max){
for(let i = min; i <= max; i++){
switch(0){
case i % 15 : console.log('FizzBuzz'); break;
case i % 3 : console.log('Fizz'); break;
case i % 5 : console.log('Buzz'); break;
default : console.log(i); break;
}
}
}
fizzBuzz(1,20)
We can use a function to find a multiple of any number and declare two variables to identify these multiples so that if you want to change the multiples you only need to change at max 2 lines of code
function isMultiple(num, mod) {
return num % mod === 0;
}
let a = 3;
let b = 5;
for(i=0;i<=100;i++){
switch(true){
case isMultiple(i,a) && isMultiple(i,b):
console.log("FizzBuzz")
case isMultiple(i,a):
console.log("Fizz");
case isMultiple(i,b):
console.log("Buzz");
default:
console.log(i);
}
}

Javascript probability

I am trying to make a realistic unbiased JavaScript dice using Math random object. I want a number from 2-12 to appear on the webpage when I click but it doesn't return. What is wrong with my code.
<html>
<head><title>DiceBoy</title>
</head>
<body>
<script>
function getRandom(){
var num=Math.random();
if(num < 0.0278) return 2;
else if(num < 0.0834) return 3;
else if(num < 0.1667) return 4;
else if(num < 0.2778) return 5;
else if(num < 0.4167) return 6;
else if(num < 0.5834) return 7;
else if(num < 0.7223) return 8;
else if(num < 0.8334) return 9;
else if(num < 0.9167) return 10;
else if(num < 0.9723) return 11;
else return 12;
var x=getRandom();
document.write(x);
}
</script>
<input type="button" value="Click Here" onClick="getRandom();">
</body>
</html>
Why not write it like this:
var RandomNumber1 = Math.floor(Math.random() * 6) + 1;
var RandomNumber2 = Math.floor(Math.random() * 6) + 1;
var DiceNumber = RandomNumber1 + RandomNumber2;
Try this:
<html>
<head>
<title>DiceBoy</title>
</head>
<body>
<script>
function getRandom() {
return (Math.floor(Math.random() * 6) + 1) * 2;
}
</script>
<input type="button" value="Click Here" onClick="document.write(getRandom());">
</body>
</html>
JSFiddle: http://jsfiddle.net/kvFE5/1/
You should ideally refactor this to be handled unobtrusively.
Although honestly - why aren't you using the Math.random...
Your return statments will exit the method when they are reached, and the rest of the code is not run. You also do not want to call your getRandom method from within the method, or you will have a never ending loop. Rather then returning the number (since no one is listening to the return value) store it in a variable and then use it in the last two lines.
function getRandom(){
var num=Math.random();
var result = 0;
if(num < 0.0278) result = 2;
else if(num < 0.0834) result = 3;
else if(num < 0.1667) result = 4;
.
.
else result = 12;
document.write(result);
}
Try something like this to generate random number between two numbers
/**
* Returns a random integer between min and max
* Using Math.round() will give you a non-uniform distribution!
*/
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
In your case call like
getRandomInt (2, 12);
The full html will look like
<html>
<head>
<script type="text/javascript">
/**
* Returns a random integer between min and max
* Using Math.round() will give you a non-uniform distribution!
*/
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function generateNumber()
{
document.getElementById('generated-id').innerHTML = getRandomInt (2, 12);
}
</script>
</head>
<body>
Generated number : <span id="generated-id"></span>
<input type="button" onclick="generateNumber();" value="Generate random number" />
</body>
</html>
You can try something like his using the Math.floor which rounds a number downward to its nearest integer.
<html>
<head>
<title>DiceBoy</title>
</head>
<body>
<script type="javascript/text">
function getDieRoll() {
return Math.floor(Math.random() * (12 - 1+ 1)) +1;
}
</script>
<input type="button" value="Click Here" onClick="document.write(getDieRoll());">
</body>
</html>
The problem is that ..
var x=getRandom();
document.write(x);
is never called as the function "returns" before then. Move the code outside the function so that it can call the randomizing function and then use the result.
function getRandom () {
// the long icky original code WITHOUT the following:
// var x=getRandom();
// document.write(x);
}
// later, OUTSIDE the function
var result = getRandom();
document.write(result);
However, a better way to get the same effect is to use:
function getDiceRole() {
return Math.floor(Math.random() * 6) + Math.floor(Math.random() * 6) + 2
}
For readers: as for why two rolls are needed, see Statistics of Dice Roles - note how it is not an equal probability to get all the values. That is, a simple Math.floor(Math.random() * 11) + 2 is incorrect.
.. in the throw of two dice, the different possibilities for the total of the two dice are not equally probable because there are more ways to get some numbers than others.

Categories

Resources