Computer Forums generatng a random decimal value
 Register FAQ Members List Calendar Search Today's Posts Mark Forums Read Log in

 Thread Tools Search this Thread Display Modes

 10-31-2014, 03: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, 06: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.

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules