# [SOLVED] Random Decimals



## Activeradio (Nov 21, 2008)

I know there is a JavaScript function called Math.random() that generates whole numbers. 

In my website, I want to generate numbers with 2 decimal points. They will need to have a max, and a minimum value. For example, 2.20, 3.56, 4.99, 1.62, or 0.99


----------



## jamiemac2005 (Jul 5, 2007)

*Re: Random Decimals*

Hey, in Javascript Math.random() returns a random number between 0 and 1... usually with a load of decimal places. Look here in the "Random number for a given range" section at the bottom of the post to see how to generate a random number between the range. And replace the "Math.floor()" call to the "Rounding to two decimal places" function given here.

Sorry i would have written up the code myself but there's a chance i'd make a minor mistake and ignore it because i'm not functionally testing it in the same way you would be.

If you do have any trouble post back and i'll work the code out and post it up.

Cheers,
Jamey


----------



## Activeradio (Nov 21, 2008)

*Re: Random Decimals*

The first link you posted does not work. In the second link, it rounds the decimals, but the numbers are not random.

This solution may be easier to code. You randomize the first numbers 1-4, and then 1-99 for the decimals.



Activeradio said:


> For example, 2.20, 3.56, 4.99, 1.62, or 0.99


----------



## jamiemac2005 (Jul 5, 2007)

*Re: Random Decimals*

Apologies about the first link i must have messed up whilst posting.

The problem with seperating the decimal places and the integer is that you risk losing accuracy and true random numbers.

The code below is the culmination of the code at both the links i attempted to post. generateNumber(max, min) will return a random number between max and min rounded to two decimal places.

```
//Generates a random number between max and min and rounds it to two decimal places.
function generateNumber(max, min){
	//get the difference between max and min
	var difference = max-min;
	//this code generates a random number between max and min
	var randomNumber = (difference*Math.random()) + min;
	//then this call rounds the number
	var result = roundNumber(randomNumber, 2);
	return result;
}

//this function rounds to the decimal points given in dec
//it is taken from http://forums.devarticles.com/javascript-development-22/javascript-to-round-to-2-decimal-places-36190.html
function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
```
Cheers,
Jamey


----------



## Activeradio (Nov 21, 2008)

*Re: Random Decimals*

That is more complicated than I need. I just want to generate numbers from a minimum, and a maximum value.

Example:

```
<script></script>.<script></script>
```
Results in:

```
2.39
```
This is much easier than rounding, and generating a random decimal.


----------



## jamiemac2005 (Jul 5, 2007)

*Re: Random Decimals*

Yeah but it will still take rounding, multiplying and then checking that the number is actually within the bounds which will result in loss of a true random number. The above script works and is the simplest way to do it. All you have to do is call the generateNumber function, inputting the maximum and minimum values and it will return a true random number rounded to two decimal places. 

Here's an (not very intense) example of the usage:
(Note, you only need to copy and paste the two functions and call generateNumber() )

```
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script language="Javascript" type="text/javascript">
//Generates a random number between max and min and rounds it to two decimal places.
function generateNumber(max, min){
	//get the difference between max and min
	var difference = max-min;
	//this code generates a random number between max and min
	var randomNumber = (difference*Math.random()) + min;
	//then this call rounds the number
	var result = roundNumber(randomNumber, 2);
	return result;
}

//this function rounds to the decimal points given in dec
//it is taken from http://forums.devarticles.com/javascript-development-22/javascript-to-round-to-2-decimal-places-36190.html
function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
</script>
</head>
<body>
<script language="Javascript" type="text/javascript">
//the line below will output a random number between 1 and 10 rounded to two decimal places
document.write(generateNumber(1, 10));
</script>
</body>
</html>
```


----------

