556

I would like to get a random value between 1 to 50 in Java.

How may I do that with the help of Math.random();?

How do I bound the values that Math.random() returns?

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Unknown user
  • 44,551
  • 16
  • 38
  • 42
  • 2
    It would be better to use Random Instead of Math.random. Random is more efficient and less biased. – kroiz May 07 '14 at 20:26
  • [`ThreadLocalRandom#nextInt( int origin , int bound )`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/ThreadLocalRandom.html#nextInt(int,int)) – Basil Bourque Mar 14 '22 at 03:39

2 Answers2

873

The first solution is to use the java.util.Random class:

import java.util.Random;

Random rand = new Random();

// Obtain a number between [0 - 49].
int n = rand.nextInt(50);

// Add 1 to the result to get a number from the required range
// (i.e., [1 - 50]).
n += 1;

Another solution is using Math.random():

double random = Math.random() * 49 + 1;

or

int random = (int)(Math.random() * 50 + 1);
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
nyanev
  • 11,299
  • 6
  • 47
  • 76
  • 5
    So if I take 45 as a minimum and `rand.nextInt(50)` returns 30, I get a value between 45 and 50? Uhm... ok... – Daniel F Mar 19 '15 at 14:36
  • 66
    @DanielF 's confusion is understandable because the comment in the answer is misleading. The 50 in `rand.nextInt(50)` will only give the maximum in this situation. `rand.nextInt(50)` will return an integer between 0 (inclusively) and 50 (exclusively) (in other words [0-49]). We add 1 to have [1-50]. So, if you take 45 as a minimum and add it to rand.nextInt(50), you'll have a value between 45 and 94 inclusively. – The_Rafi Apr 02 '16 at 03:57
  • 8
    @The_Rafi Indeed. `rand.nextInt(1)` will only return 0, not 1 or 0. – Erik Humphrey Mar 08 '17 at 15:45
  • 1
    CAUTION!! util.Random has implemented at Java 8. – Tsakiroglou Fotis Jun 13 '17 at 16:10
  • 1
    it will give output 1 to 49 – Syed Hussain Mehdi Feb 22 '18 at 09:36
  • 1
    So rand.nextInt(6) + 45 would generate random number 45-50 – Popeye Apr 10 '18 at 19:20
  • @Popeye Yes. rand.nextInt(6) will give you [0-5] and adding 45 to the result makes it [45-50] – Sanved Feb 11 '19 at 03:28
  • What if I want to generate two numbers, first number I have generated say 34 and now I want to generate one more random numbers excluding number 34. The thing is when I have input list which has very minimum element( note: the list is dynamic) – Kavita Kulkarni Jun 14 '21 at 12:38
623
int max = 50;
int min = 1;

1. Using Math.random()

double random = Math.random() * 49 + 1;
or
int random = (int )(Math.random() * 50 + 1);

This will give you value from 1 to 50 in case of int or 1.0 (inclusive) to 50.0 (exclusive) in case of double

Why?

random() method returns a random number between 0.0 and 0.9..., you multiply it by 50, so upper limit becomes 0.0 to 49.999... when you add 1, it becomes 1.0 to 50.999..., now when you truncate to int, you get 1 to 50. (thanks to @rup in comments). leepoint's awesome write-up on both the approaches.

2. Using Random class in Java.

Random rand = new Random(); 
int value = rand.nextInt(50); 

This will give value from 0 to 49.

For 1 to 50: rand.nextInt((max - min) + 1) + min;

Source of some Java Random awesomeness.

Dave Deasy
  • 316
  • 2
  • 11
zengr
  • 38,346
  • 37
  • 130
  • 192
  • 1
    "0.0 to 50.0, when you add 1, it becomes 1.0 to 50.0" is surely not correct? There must be 49 or a 51 somewhere there. – Blorgbeard May 05 '11 at 12:47
  • 9
    @Blorgbeard the quote's wrong; the result is greater-than-or-equal-to 0 but strictly less than 1 ([documentation](http://download.oracle.com/javase/6/docs/api/java/lang/Math.html#random() ). So it's 0.0 to 49.999 etc. which becomes 1 to 50.999 etc. when you add 1, which becomes 1 to 50 when you truncate to int. – Rup May 05 '11 at 13:48
  • it did not work for all ranges for example .. it gives me 31 when i tried to get a number between 28 and 7 – maysara Nov 04 '16 at 14:00
  • @Maysara I updated the 2nd example to handle random ranges. The specific example was to use from 1-50. – zengr Nov 04 '16 at 18:21
  • if you want numbers between 8 and 50 the first version you will get values between 8 and 58. You would need a formula like this to get it right . . . . . . (int)(Math.random() * (50-8) + 8) – Manuel Hernandez Jul 10 '17 at 19:08