---
title: "Generating random number in a range with Java"  
description: "Generating random number in a range with Java"  
author: "Anonymous User"  
published: 2013-05-07  
updated: 2013-05-07  
canonical: https://www.mindstick.com/forum/829/generating-random-number-in-a-range-with-java  
category: "mssql server"  
tags: ["mssql server"]  
reading_time: 2 minutes  

---

# Generating random number in a range with Java

Hi Everyone!\
I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to generate a [random](https://www.mindstick.com/forum/33419/generating-random-numbers-in-objective-c) number with Java, but random in a specific range. For example, my range is 5-10, meaning that 5 is the [smallest](https://yourviews.mindstick.com/view/81880/royal-families-of-world-s-smallest-islands-do-fishing) possible value the \
random number can take, and 10 is the biggest. Any other number in between these numbers is possible to be a value, too.\
In Java, there is a [method](https://www.mindstick.com/forum/166/webservice-method) random() in the Math [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp), which returns a double value between 0.0 and 1.0. In the class Random there is a method nextInt(int n), which \
returns a random value in the range of 0 (inclusive) and n (exclusive). I couldn't find a method, which returns a random value between two numbers.\
I have tried the following things, but I still have problems: ([minimum and maximum](https://answers.mindstick.com/qa/93254/what-should-be-the-minimum-and-maximum-word-limit-of-an-article) are the smallest and biggest numbers).\
**Solution 1 :**\
randomNum = minimum + (int)(Math.random()*maximum); [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic): randomNum takes is assinged [values](https://www.mindstick.com/forum/327/sum-textbox-values) numbers bigger that maximum\
**Solution 2 :**\
Random rn = new Random();int n = maximum - minimum + 1;int i = rn.nextInt() % n;randomNum = minimum + i;problem: randomNum takes is assigned values smaller than minimum.\
Could you suggest how to solve my problem, or point me to some references?\
Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)!

## Replies

### Reply by AVADHESH PATEL

Hi Chinto!\
this is what i do:-I just generate a random number using Math.random() and multiply it by a big number...lets say 10000.So, i get a number between 0 to 10,000 and call this number i.Now, If i need numbers between (x, y), then do the following:\
i = x + (i % (y - x));So, all i are numbers between x and y.\
EDIT: To remove the bias as pointed out in the comments, rather than multiplying it by 10000 (or the big number), multiply it by (y-x)


---

Original Source: https://www.mindstick.com/forum/829/generating-random-number-in-a-range-with-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
