T.script - 1 year ago 91

Javascript Question

I want to generate a random number between 1 and 10 up to 2 decimal places,

I'm currently using this below to generate my numbers,

`var randomnum = Math.floor(Math.random() * (10.00 - 1.00 + 1.00)) + 1.00;`

Ultimately, I would like to know how to generate numbers like:

1.66

5.86

8.34

In the format:

sidenote: I don't remember why I previously had generated my numbers like that but remember something about

Thank you for the help! :)

Ps: I've seen a lot of posts about waiting to round down or up generated numbers and haven't found one wanting to generate them straight out.

UPDATE: I want a number value not a string that looks like a number

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You were very close, what you need is not to work with decimal numbers as min and max. Let's have max = 1000 and min = 100, so after your Math.floor you will need to divide by 100:

```
var randomnum = Math.floor(Math.random() * (1000 - 100) + 100) / 100;
```

Or if you want to work with decimals:

```
var precision = 100; // 2 decimals
var randomnum = Math.floor(Math.random() * (10 * precision - 1 * precision) + 1 * precision) / (1*precision);
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**