134

This is a pretty simple Java (though probably applicable to all programming) question:

Math.random() returns a number between zero and one.

If I want to return an integer between zero and hundred, I would do:

(int) Math.floor(Math.random() * 101)

Between one and hundred, I would do:

(int) Math.ceil(Math.random() * 100)

But what if I wanted to get a number between three and five? Will it be like following statement:

(int) Math.random() * 5 + 3

I know about nextInt() in java.lang.util.Random. But I want to learn how to do this with Math.random().

burntblark
  • 1,680
  • 1
  • 15
  • 25
switz
  • 24,384
  • 25
  • 76
  • 101

5 Answers5

164
int randomWithRange(int min, int max)
{
   int range = (max - min) + 1;     
   return (int)(Math.random() * range) + min;
}

Output of randomWithRange(2, 5) 10 times:

5
2
3
3
2
4
4
4
5
4

The bounds are inclusive, ie [2,5], and min must be less than max in the above example.

EDIT: If someone was going to try and be stupid and reverse min and max, you could change the code to:

int randomWithRange(int min, int max)
{
   int range = Math.abs(max - min) + 1;     
   return (int)(Math.random() * range) + (min <= max ? min : max);
}

EDIT2: For your question about doubles, it's just:

double randomWithRange(double min, double max)
{
   double range = (max - min);     
   return (Math.random() * range) + min;
}

And again if you want to idiot-proof it it's just:

double randomWithRange(double min, double max)
{
   double range = Math.abs(max - min);     
   return (Math.random() * range) + (min <= max ? min : max);
}
AusCBloke
  • 18,014
  • 6
  • 40
  • 44
  • 1
    What if you don't use `(int)` and want it to return a `double`? – switz Nov 01 '11 at 02:36
  • If you want double then just replace the `int`s with `double`s (and the typecast is unnecessary). I assumed you wanted `int`s but I'll add to my post. – AusCBloke Nov 01 '11 at 02:40
  • 2
    Actually with `double`s remove the `+ 1` also since `Math.random()` isn't being truncated. However, the range will be [min, max) since `Math.random` "Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0." There'd be a very minimal chance of the number being exactly `max` anyway even if it was possible. – AusCBloke Nov 01 '11 at 02:47
45

If you want to generate a number from 0 to 100, then your code would look like this:

(int)(Math.random() * 101);

To generate a number from 10 to 20 :

(int)(Math.random() * 11 + 10);

In the general case:

(int)(Math.random() * ((upperbound - lowerbound) + 1) + lowerbound);

(where lowerbound is inclusive and upperbound exclusive).

The inclusion or exclusion of upperbound depends on your choice. Let's say range = (upperbound - lowerbound) + 1 then upperbound is inclusive, but if range = (upperbound - lowerbound) then upperbound is exclusive.

Example: If I want an integer between 3-5, then if range is (5-3)+1 then 5 is inclusive, but if range is just (5-3) then 5 is exclusive.

Teezy7
  • 515
  • 5
  • 9
19

The Random class of Java located in the java.util package will serve your purpose better. It has some nextInt() methods that return an integer. The one taking an int argument will generate a number between 0 and that int, the latter not inclusive.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
G_H
  • 11,739
  • 3
  • 38
  • 82
1

To generate a number between 10 to 20 inclusive, you can use java.util.Random

int myNumber = new Random().nextInt(11) + 10
Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
0

Here's a method which receives boundaries and returns a random integer. It is slightly more advanced (completely universal): boundaries can be both positive and negative, and minimum/maximum boundaries can come in any order.

int myRand(int i_from, int i_to) {
  return (int)(Math.random() * (Math.abs(i_from - i_to) + 1)) + Math.min(i_from, i_to);
}

In general, it finds the absolute distance between the borders, gets relevant random value, and then shifts the answer based on the bottom border.

fault-tolerant
  • 451
  • 5
  • 15