Computer Forums generatng a random decimal value

 10-31-2014, 04:26 PM #1 Baseband Member   Join Date: Jul 2014 Location: USA Posts: 29 generatng a random decimal value I know that when you use the math.random() that it generates a random value with many places : 5.6938506849, and I need it to just generate two decimal value: 5.69. How do I do this, here is a portion of the code: //problem 1 var num1a = (Math.random()* 10) + 1; var num1b = (Math.random()* 10) + 1; var product1 = num1a * num1b; var response = parseFloat(prompt("What is the product of " + num1a + " and " + num1b + " ?")); if (response == product1) { count1 = count1 + 1; result = "correct!"; alert(result); } else { result = "incorrect"; alert(result); } if (count1 == 3) { levelTwo(); return; } Thank you! __________________ __________________
 11-07-2014, 07:06 PM #2 Fully Optimized     Join Date: Nov 2014 Location: United States Posts: 1,653 Re: generatng a random decimal value Wrap your Math.Random functions in Math.Floor()... I have altered your script to reflect this: //problem 1 var num1a = Math.floor((Math.random() * 10) + 1); var num1b = Math.floor((Math.random() * 10) + 1); var product1 = num1a * num1b; var response = parseFloat(prompt("What is the product of " + num1a + " and " + num1b + " ?")); if (response == product1) { count1 = count1 + 1; result = "correct!"; alert(result); } else { result = "incorrect"; alert(result); } if (count1 == 3) { levelTwo(); return; } It will return a single digit value, without the need for decimals at all. __________________ Will have a youtube channel up soon. Link will be here if I remember.