## To get the random number

The Math.random() function returns a floating-point, pseudo-random number in the range [0, 1); that is, from 0 (inclusive) up to but not including 1 (exclusive)

`add`
`randomly`
`max-min`
`max`
`min`

The above result X is a random numeric. However due to Math.random() our left bound is inclusive, and the right bound is exclusive. To include our right bound we increase the right bound by 1 and floor the result.

```function generateRandomInteger(min, max) {
return Math.floor(min + Math.random()*(max+1 - min))
}```
`generateRandomInteger(-20, 20)`

## Generate random number between two numbers in JavaScript - Stack Overf...

javascript random

## To get the random number

Math.random() would always give between 0 and 1. Logic is to reduce the max randomly from 0 to max-min

`max`
`min`

max - r * (max-min) = X, where X has range of min < X < max

```function generateRandomInteger(min, max) {
return Math.floor(max - Math.random()*(max-min))
}
// Similarly for Numeric
function generateRandomNumeric(min, max) {
return max - Math.random()*(max-min)
}```

The solution works for use-cases as below. I am listing them in detail as the accepted answer and other solutions failed in some of them.

`generateRandomInteger(100, 200)`
`generateRandomInteger(0, 200)`
`generateRandomInteger(1, 200)`
`generateRandomNumeric(0, 1)`
`generateRandomInteger(-20, 0)`
`generateRandomInteger(-20, -1)`
`generateRandomInteger(-20, -10)`
• Between negative integers and positive integers , ex -20, 20 generateRandomInteger(-20, 20)

## Generate random number between two numbers in JavaScript - Stack Overf...

javascript random

## To get the random number

Math.random() would always give between 0 and 1. Logic is to reduce the max randomly from 0 to max-min

`max`
`min`

max - r * (max-min) = X, where X has range of min < X < max

```function generateRandomInteger(min, max) {
return Math.floor(max - Math.random()*(max-min))
}
// Similarly for Numeric
function generateRandomNumeric(min, max) {
return max - Math.random()*(max-min)
}```

The solution works for use-cases as below. I am listing them in detail as the accepted answer and other solutions failed in some of them.

`generateRandomInteger(100, 200)`
`generateRandomInteger(0, 200)`
`generateRandomInteger(1, 200)`
`generateRandomNumeric(0, 1)`
`generateRandomInteger(-20, 0)`
`generateRandomInteger(-20, -1)`
`generateRandomInteger(-20, -10)`
• Between negative integers and positive integers , ex -20, 20 generateRandomInteger(-20, 20)

## Generate random number between two numbers in JavaScript - Stack Overf...

javascript random

## To get the random number

Math.random() would always give between 0 and 1. Logic is to reduce the max randomly from 0 to max-min

`max`
`min`

max - r * (max-min) = X, where X has range of min < X < max

```function generateRandomInteger(min, max) {
return Math.floor(max - Math.random()*(max-min))
}
// Similarly for Numeric
function generateRandomNumeric(min, max) {
return max - Math.random()*(max-min)
}```

The solution works for use-cases as below. I am listing them in detail as the accepted answer and other solutions failed in some of them.

`generateRandomInteger(100, 200)`
`generateRandomInteger(0, 200)`
`generateRandomInteger(1, 200)`
`generateRandomNumeric(0, 1)`
`generateRandomInteger(-20, 0)`
`generateRandomInteger(-20, -1)`
`generateRandomInteger(-20, -10)`
• Between negative integers and positive integers , ex -20, 20 generateRandomInteger(-20, 20)

## Generate random number between two numbers in JavaScript - Stack Overf...

javascript random

## To get the random number

Math.random() would always give between 0 and 1. Logic is to reduce the max randomly from 0 to max-min

`max`
`min`

max - r * (max-min) = X, where X has range of min < X < max

```function generateRandomInteger(min, max) {
return Math.floor(max - Math.random()*(max-min))
}
// Similarly for Numeric
function generateRandomNumeric(min, max) {
return max - Math.random()*(max-min)
}```

The solution works for use-cases as below. I am listing them in detail as the accepted answer and other solutions failed in some of them.

`generateRandomInteger(100, 200)`
`generateRandomInteger(0, 200)`
`generateRandomInteger(1, 200)`
`generateRandomNumeric(0, 1)`
`generateRandomInteger(-20, 0)`
`generateRandomInteger(-20, -1)`
`generateRandomInteger(-20, -10)`
• Between negative integers and positive integers , ex -20, 20 generateRandomInteger(-20, 20)

## Generate random number between two numbers in JavaScript - Stack Overf...

javascript random

## To get the random number

Math.random() would always give between 0 and 1. Logic is to reduce the max randomly from 0 to max-min

`max`
`min`

max - r * (max-min) = X, where X has range of min < X < max

```function generateRandomInteger(min, max) {
return Math.floor(max - Math.random()*(max-min))
}
// Similarly for Numeric
function generateRandomNumeric(min, max) {
return max - Math.random()*(max-min)
}```

The solution works for use-cases as below. I am listing them in detail as the accepted answer and other solutions failed in some of them.

`generateRandomInteger(100, 200)`
`generateRandomInteger(0, 200)`
`generateRandomInteger(1, 200)`
`generateRandomNumeric(0, 1)`
`generateRandomInteger(-20, 0)`
`generateRandomInteger(-20, -1)`
`generateRandomInteger(-20, -10)`
• Between negative integers and positive integers , ex -20, 20 generateRandomInteger(-20, 20)

## Generate random number between two numbers in JavaScript - Stack Overf...

javascript random

While you can use rand(42-10) + 10 to get a random number between 10 and 42 (where 10 is inclusive and 42 exclusive), there's a better way since Ruby 1.9.3, where you are able to call:

`rand(10...42) # => 13`

Available for all versions of Ruby by requiring my backports gem.

Ruby 1.9.2 also introduced the Random class so you can create your own random number generator objects and has a nice API:

```r = Random.new
r.rand(10...42) # => 22
r.bytes(3) # => "rnd"```

The Random class itself acts as a random generator, so you call directly:

`Random.rand(10...42) # => same as rand(10...42)`

In most cases, the simplest is to use rand or Random.rand. Creating a new random generator each time you want a random number is a really bad idea. If you do this, you will get the random properties of the initial seeding algorithm which are atrocious compared to the properties of the random generator itself.

`Random.new`
`MyApp::Random = Random.new`

The cases where Random.new is helpful are the following:

• you are writing a gem and don't want to interfere with the sequence of rand/Random.rand that the main programs might be relying on
• you want separate reproducible sequences of random numbers (say one per thread)
• you want to be able to save and resume a reproducible sequence of random numbers (easy as Random objects can marshalled)

How does the backports gem work, in broad strokes?

@yar: My backports gem is simply a collection of methods that are new to RUby 1.8.7, 1.9.1, 1.9.2, but implemented in Ruby. I use RubySpec to insure that the results are compatible with Ruby.

Random.rand(10..42) does not work. The Random.rand class method does not accept a range. (Ruby 1.9.2p180)

@banister: wow, I was convinced that the new api (rand with range, bytes, etc...) was available directly through the Random object. rand with range will be in 1.9.3, and I'll make a feature request for bytes. I've edited my answer

## How to get a random number in Ruby - Stack Overflow

ruby random

While you can use rand(42-10) + 10 to get a random number between 10 and 42 (where 10 is inclusive and 42 exclusive), there's a better way since Ruby 1.9.3, where you are able to call:

`rand(10...42) # => 13`

Available for all versions of Ruby by requiring my backports gem.

Ruby 1.9.2 also introduced the Random class so you can create your own random number generator objects and has a nice API:

```r = Random.new
r.rand(10...42) # => 22
r.bytes(3) # => "rnd"```

The Random class itself acts as a random generator, so you call directly:

`Random.rand(10...42) # => same as rand(10...42)`

In most cases, the simplest is to use rand or Random.rand. Creating a new random generator each time you want a random number is a really bad idea. If you do this, you will get the random properties of the initial seeding algorithm which are atrocious compared to the properties of the random generator itself.

`Random.new`
`MyApp::Random = Random.new`

The cases where Random.new is helpful are the following:

• you are writing a gem and don't want to interfere with the sequence of rand/Random.rand that the main programs might be relying on
• you want separate reproducible sequences of random numbers (say one per thread)
• you want to be able to save and resume a reproducible sequence of random numbers (easy as Random objects can marshalled)

How does the backports gem work, in broad strokes?

@yar: My backports gem is simply a collection of methods that are new to RUby 1.8.7, 1.9.1, 1.9.2, but implemented in Ruby. I use RubySpec to insure that the results are compatible with Ruby.

Random.rand(10..42) does not work. The Random.rand class method does not accept a range. (Ruby 1.9.2p180)

@banister: wow, I was convinced that the new api (rand with range, bytes, etc...) was available directly through the Random object. rand with range will be in 1.9.3, and I'll make a feature request for bytes. I've edited my answer

## How to get a random number in Ruby - Stack Overflow

ruby random

While you can use rand(42-10) + 10 to get a random number between 10 and 42 (where 10 is inclusive and 42 exclusive), there's a better way since Ruby 1.9.3, where you are able to call:

`rand(10...42) # => 13`

Available for all versions of Ruby by requiring my backports gem.

Ruby 1.9.2 also introduced the Random class so you can create your own random number generator objects and has a nice API:

```r = Random.new
r.rand(10...42) # => 22
r.bytes(3) # => "rnd"```

The Random class itself acts as a random generator, so you call directly:

`Random.rand(10...42) # => same as rand(10...42)`

In most cases, the simplest is to use rand or Random.rand. Creating a new random generator each time you want a random number is a really bad idea. If you do this, you will get the random properties of the initial seeding algorithm which are atrocious compared to the properties of the random generator itself.

`Random.new`
`MyApp::Random = Random.new`

The cases where Random.new is helpful are the following:

• you are writing a gem and don't want to interfere with the sequence of rand/Random.rand that the main programs might be relying on
• you want separate reproducible sequences of random numbers (say one per thread)
• you want to be able to save and resume a reproducible sequence of random numbers (easy as Random objects can marshalled)

How does the backports gem work, in broad strokes?

@yar: My backports gem is simply a collection of methods that are new to RUby 1.8.7, 1.9.1, 1.9.2, but implemented in Ruby. I use RubySpec to insure that the results are compatible with Ruby.

Random.rand(10..42) does not work. The Random.rand class method does not accept a range. (Ruby 1.9.2p180)

@banister: wow, I was convinced that the new api (rand with range, bytes, etc...) was available directly through the Random object. rand with range will be in 1.9.3, and I'll make a feature request for bytes. I've edited my answer

## How to get a random number in Ruby - Stack Overflow

ruby random

While you can use rand(42-10) + 10 to get a random number between 10 and 42 (where 10 is inclusive and 42 exclusive), there's a better way since Ruby 1.9.3, where you are able to call:

`rand(10...42) # => 13`

Available for all versions of Ruby by requiring my backports gem.

Ruby 1.9.2 also introduced the Random class so you can create your own random number generator objects and has a nice API:

```r = Random.new
r.rand(10...42) # => 22
r.bytes(3) # => "rnd"```

The Random class itself acts as a random generator, so you call directly:

`Random.rand(10...42) # => same as rand(10...42)`

In most cases, the simplest is to use rand or Random.rand. Creating a new random generator each time you want a random number is a really bad idea. If you do this, you will get the random properties of the initial seeding algorithm which are atrocious compared to the properties of the random generator itself.

`Random.new`
`MyApp::Random = Random.new`

The cases where Random.new is helpful are the following:

• you are writing a gem and don't want to interfere with the sequence of rand/Random.rand that the main programs might be relying on
• you want separate reproducible sequences of random numbers (say one per thread)
• you want to be able to save and resume a reproducible sequence of random numbers (easy as Random objects can marshalled)

How does the backports gem work, in broad strokes?

@yar: My backports gem is simply a collection of methods that are new to RUby 1.8.7, 1.9.1, 1.9.2, but implemented in Ruby. I use RubySpec to insure that the results are compatible with Ruby.

Random.rand(10..42) does not work. The Random.rand class method does not accept a range. (Ruby 1.9.2p180)

@banister: wow, I was convinced that the new api (rand with range, bytes, etc...) was available directly through the Random object. rand with range will be in 1.9.3, and I'll make a feature request for bytes. I've edited my answer

## How to get a random number in Ruby - Stack Overflow

ruby random

Every time you do new Random() it is initialized using the clock. This means that in a tight loop you get the same value lots of times. You should keep a single Random instance and keep using Next on the same instance.

```//Function to get a random number
private static readonly Random random = new Random();
private static readonly object syncLock = new object();
public static int RandomNumber(int min, int max)
{
lock(syncLock) { // synchronize
return random.Next(min, max);
}
}```

Edit (see comments): why do we need a lock here?

Basically, Next is going to change the internal state of the Random instance. If we do that at the same time from multiple threads, you could argue "we've just made the outcome even more random", but what we are actually doing is potentially breaking the internal implementation, and we could also start getting the same numbers from different threads, which might be a problem - and might not. The guarantee of what happens internally is the bigger issue, though; since Random does not make any guarantees of thread-safety. Thus there are two valid approaches:

• synchronize so that we don't access it at the same time from different threads
`Random`

Either can be fine; but mutexing a single instance from multiple callers at the same time is just asking for trouble.

The lock achieves the first (and simpler) of these approaches; however, another approach might be:

```private static readonly ThreadLocal<Random> appRandom
= new ThreadLocal<Random>(() => new Random());```

@Rekreativc - no, re-read it: it is working only when there are break-points, which cause a delay and thus provide different seeds. Without the break-points, everything is equal, i.e. not random, i.e. broken.

Certainly; it synchronizes access, so that if multiple threads are calling your static "RandomNumber" method, they don't trip over each-other and cause an error. Only one thread will ever be inside the "lock" statement at once, since they are all sharing a single lock object (syncLock).

As a general rule, all static methods should be made thread-safe, since it is hard to guarantee that multiple threads won't call it at the same time. It is not usually necessary to make instance (i.e. non-static) methods thread-safe.

@Florin - there is no difference re "stack based" between the two. Static fields are just as much "external state", and will absolutely be shared between callers. With instances, there is a good chance that different threads have different instances (a common pattern). With statics, it is guaranteed that they all share (not including [ThreadStatic]).

@Dan if the object is never exposed publicly: you can. The (very theoretical) risk is that some other thread is locking on it in ways you did not expect.

## c# - Random number generator only generating one random number - Stack...

c# random

Every time you do new Random() it is initialized using the clock. This means that in a tight loop you get the same value lots of times. You should keep a single Random instance and keep using Next on the same instance.

```//Function to get a random number
private static readonly Random random = new Random();
private static readonly object syncLock = new object();
public static int RandomNumber(int min, int max)
{
lock(syncLock) { // synchronize
return random.Next(min, max);
}
}```

Edit (see comments): why do we need a lock here?

Basically, Next is going to change the internal state of the Random instance. If we do that at the same time from multiple threads, you could argue "we've just made the outcome even more random", but what we are actually doing is potentially breaking the internal implementation, and we could also start getting the same numbers from different threads, which might be a problem - and might not. The guarantee of what happens internally is the bigger issue, though; since Random does not make any guarantees of thread-safety. Thus there are two valid approaches:

• synchronize so that we don't access it at the same time from different threads
`Random`

Either can be fine; but mutexing a single instance from multiple callers at the same time is just asking for trouble.

The lock achieves the first (and simpler) of these approaches; however, another approach might be:

```private static readonly ThreadLocal<Random> appRandom
= new ThreadLocal<Random>(() => new Random());```

@Rekreativc - no, re-read it: it is working only when there are break-points, which cause a delay and thus provide different seeds. Without the break-points, everything is equal, i.e. not random, i.e. broken.

Certainly; it synchronizes access, so that if multiple threads are calling your static "RandomNumber" method, they don't trip over each-other and cause an error. Only one thread will ever be inside the "lock" statement at once, since they are all sharing a single lock object (syncLock).

As a general rule, all static methods should be made thread-safe, since it is hard to guarantee that multiple threads won't call it at the same time. It is not usually necessary to make instance (i.e. non-static) methods thread-safe.

@Florin - there is no difference re "stack based" between the two. Static fields are just as much "external state", and will absolutely be shared between callers. With instances, there is a good chance that different threads have different instances (a common pattern). With statics, it is guaranteed that they all share (not including [ThreadStatic]).

@Dan if the object is never exposed publicly: you can. The (very theoretical) risk is that some other thread is locking on it in ways you did not expect.

## c# - Random number generator only generating one random number - Stack...

c# random

Every time you do new Random() it is initialized using the clock. This means that in a tight loop you get the same value lots of times. You should keep a single Random instance and keep using Next on the same instance.

```//Function to get a random number
private static readonly Random random = new Random();
private static readonly object syncLock = new object();
public static int RandomNumber(int min, int max)
{
lock(syncLock) { // synchronize
return random.Next(min, max);
}
}```
```//Function to get random number
private static readonly Random random = new Random();
private static readonly object syncLock = new object();
public static int RandomNumber(int min, int max)
{
lock(syncLock) { // synchronize
return random.Next(min, max);
}
}```

Edit (see comments): why do we need a lock here?

Basically, Next is going to change the internal state of the Random instance. If we do that at the same time from multiple threads, you could argue "we've just made the outcome even more random", but what we are actually doing is potentially breaking the internal implementation, and we could also start getting the same numbers from different threads, which might be a problem - and might not. The guarantee of what happens internally is the bigger issue, though; since Random does not make any guarantees of thread-safety. Thus there are two valid approaches:

• synchronize so that we don't access it at the same time from different threads
`Random`

either can be fine; but mutating a single instance from multiple callers at the same time is just asking for trouble.

Either can be fine; but mutexing a single instance from multiple callers at the same time is just asking for trouble.

The lock achieves the first (and simpler) of these approaches; however, another approach might be:

```private static readonly ThreadLocal<Random> appRandom
= new ThreadLocal<Random>(() => new Random());```

@Rekreativc - no, re-read it: it is working only when there are break-points, which cause a delay and thus provide different seeds. Without the break-points, everything is equal, i.e. not random, i.e. broken.

Certainly; it synchronizes access, so that if multiple threads are calling your static "RandomNumber" method, they don't trip over each-other and cause an error. Only one thread will ever be inside the "lock" statement at once, since they are all sharing a single lock object (syncLock).

As a general rule, all static methods should be made thread-safe, since it is hard to guarantee that multiple threads won't call it at the same time. It is not usually necessary to make instance (i.e. non-static) methods thread-safe.

@Florin - there is no difference re "stack based" between the two. Static fields are just as much "external state", and will absolutely be shared between callers. With instances, there is a good chance that different threads have different instances (a common pattern). With statics, it is guaranteed that they all share (not including [ThreadStatic]).

@Dan if the object is never exposed publicly: you can. The (very theoretical) risk is that some other thread is locking on it in ways you did not expect.

## c# - Random number generator only generating one random number - Stack...

c# random

Every time you do new Random() it is initialized using the clock. This means that in a tight loop you get the same value lots of times. You should keep a single Random instance and keep using Next on the same instance.

```//Function to get a random number
private static readonly Random random = new Random();
private static readonly object syncLock = new object();
public static int RandomNumber(int min, int max)
{
lock(syncLock) { // synchronize
return random.Next(min, max);
}
}```

Edit (see comments): why do we need a lock here?

Basically, Next is going to change the internal state of the Random instance. If we do that at the same time from multiple threads, you could argue "we've just made the outcome even more random", but what we are actually doing is potentially breaking the internal implementation, and we could also start getting the same numbers from different threads, which might be a problem - and might not. The guarantee of what happens internally is the bigger issue, though; since Random does not make any guarantees of thread-safety. Thus there are two valid approaches:

• synchronize so that we don't access it at the same time from different threads
`Random`

Either can be fine; but mutexing a single instance from multiple callers at the same time is just asking for trouble.

The lock achieves the first (and simpler) of these approaches; however, another approach might be:

```private static readonly ThreadLocal<Random> appRandom
= new ThreadLocal<Random>(() => new Random());```

@Rekreativc - no, re-read it: it is working only when there are break-points, which cause a delay and thus provide different seeds. Without the break-points, everything is equal, i.e. not random, i.e. broken.

Certainly; it synchronizes access, so that if multiple threads are calling your static "RandomNumber" method, they don't trip over each-other and cause an error. Only one thread will ever be inside the "lock" statement at once, since they are all sharing a single lock object (syncLock).

As a general rule, all static methods should be made thread-safe, since it is hard to guarantee that multiple threads won't call it at the same time. It is not usually necessary to make instance (i.e. non-static) methods thread-safe.

@Florin - there is no difference re "stack based" between the two. Static fields are just as much "external state", and will absolutely be shared between callers. With instances, there is a good chance that different threads have different instances (a common pattern). With statics, it is guaranteed that they all share (not including [ThreadStatic]).

@Dan if the object is never exposed publicly: you can. The (very theoretical) risk is that some other thread is locking on it in ways you did not expect.

## c# - Random number generator only generating one random number - Stack...

c# random

To get a random number say between 1 and 6, first do:

`0.5 + (Math.random() * ((6 - 1) + 1))`

This multiplies a random number by 6 and then adds 0.5 to it. Next round the number to a positive integer by doing:

`Math.round(0.5 + (Math.random() * ((6 - 1) + 1))`

This round the number to the nearest whole number.

```var value = 0.5 + (Math.random() * ((6 - 1) + 1))
var roll = Math.round(value);
return roll;```

In general the code for doing this using variables is:

```var value = (Min - 0.5) + (Math.random() * ((Max - Min) + 1))
var roll = Math.round(value);
return roll;```

The reason for taking away 0.5 from the minimum value is because using the minimum value alone would allow you to get an integer that was one more than your maximum value. By taking away 0.5 from the minimum value you are essentially preventing the maximum value from being rounded up.

Make sense if you're excluding 0, then no need to "round down" range from 0 to 0.5.

## Generating random whole numbers in JavaScript in a specific range? - S...

javascript random integer

To get a random number say between 1 and 6, first do:

`0.5 + (Math.random() * ((6 - 1) + 1))`

This multiplies a random number by 6 and then adds 0.5 to it. Next round the number to a positive integer by doing:

`Math.round(0.5 + (Math.random() * ((6 - 1) + 1))`

This round the number to the nearest whole number.

```var value = 0.5 + (Math.random() * ((6 - 1) + 1))
var roll = Math.round(value);
return roll;```

In general the code for doing this using variables is:

```var value = (Min - 0.5) + (Math.random() * ((Max - Min) + 1))
var roll = Math.round(value);
return roll;```

The reason for taking away 0.5 from the minimum value is because using the minimum value alone would allow you to get an integer that was one more than your maximum value. By taking away 0.5 from the minimum value you are essentially preventing the maximum value from being rounded up.

Make sense if you're excluding 0, then no need to "round down" range from 0 to 0.5.

## Generating random whole numbers in JavaScript in a specific range? - S...

javascript random integer

To get a random number say between 1 and 6, first do:

`0.5 + (Math.random() * ((6 - 1) + 1))`

This multiplies a random number by 6 and then adds 0.5 to it. Next round the number to a positive integer by doing:

`Math.round(0.5 + (Math.random() * ((6 - 1) + 1))`

This round the number to the nearest whole number.

```var value = 0.5 + (Math.random() * ((6 - 1) + 1))
var roll = Math.round(value);
return roll;```

In general the code for doing this using variables is:

```var value = (Min - 0.5) + (Math.random() * ((Max - Min) + 1))
var roll = Math.round(value);
return roll;```

The reason for taking away 0.5 from the minimum value is because using the minimum value alone would allow you to get an integer that was one more than your maximum value. By taking away 0.5 from the minimum value you are essentially preventing the maximum value from being rounded up.

Make sense if you're excluding 0, then no need to "round down" range from 0 to 0.5.

## Generating random whole numbers in JavaScript in a specific range? - S...

javascript random integer

To get a random number say between 1 and 6, first do:

`0.5 + (Math.random() * ((6 - 1) + 1))`

This multiplies a random number by 6 and then adds 0.5 to it. Next round the number to a positive integer by doing:

`Math.round(0.5 + (Math.random() * ((6 - 1) + 1))`

This round the number to the nearest whole number.

```var value = 0.5 + (Math.random() * ((6 - 1) + 1))
var roll = Math.round(value);
return roll;```

In general the code for doing this using variables is:

```var value = (Min - 0.5) + (Math.random() * ((Max - Min) + 1))
var roll = Math.round(value);
return roll;```

The reason for taking away 0.5 from the minimum value is because using the minimum value alone would allow you to get an integer that was one more than your maximum value. By taking away 0.5 from the minimum value you are essentially preventing the maximum value from being rounded up.

Make sense if you're excluding 0, then no need to "round down" range from 0 to 0.5.

## Generating random whole numbers in JavaScript in a specific range? - S...

javascript random integer
```function generateRandomNumber() {
var min = 0.0200,
max = 0.120,
highlightedNumber = Math.random() * (max - min) + min;

};

generateRandomNumber();```

I understand your real question you do not know how to get a random number between floating numbers, right? By the way, the answer is passed before.

To get the four first numbers of your decimal, use .toFixed(3) method. I've performed an example here, on jsFiddle.

Is it possible to round, only showing say 3 decimal places? I thought .toFixed would work but it hasn't.

`highlightedNumber = (Math.random() * (max - min) + min).toFixed(4);`

I made an update. @Edit: Oh, sorry. I didn't saw the accepted answer.

## javascript - Get a random number between 0.0200 and 0.120 (float numbe...

javascript
```function generateRandomNumber() {
var min = 0.0200,
max = 0.120,
highlightedNumber = Math.random() * (max - min) + min;

};

generateRandomNumber();```

I understand your real question you do not know how to get a random number between floating numbers, right? By the way, the answer is passed before.

To get the four first numbers of your decimal, use .toFixed(3) method. I've performed an example here, on jsFiddle.

Is it possible to round, only showing say 3 decimal places? I thought .toFixed would work but it hasn't.

`highlightedNumber = (Math.random() * (max - min) + min).toFixed(4);`

I made an update. @Edit: Oh, sorry. I didn't saw the accepted answer.

javascript