Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 10-31-2014, 04:26 PM   #1
Baseband Member
 
Join Date: Jul 2014
Location: USA
Posts: 29
Default 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!
__________________

megan1989 is offline   Reply With Quote
Old 11-07-2014, 07:06 PM   #2
Fully Optimized
 
Draygoes's Avatar
 
Join Date: Nov 2014
Location: United States
Posts: 1,653
Default 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.
Draygoes is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 02:00 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0