How to generate random integers within specific range in Java

+1 vote
The specific range is given and I want to print random integers within given range. How to do it?
Apr 13, 2018 in Java by Daisy
• 8,140 points
1,253 views

2 answers to this question.

0 votes

To generate random integers within a specific range, I have tried the code given below.

public class RandomIntegers {

public static void main(String[] args) {

System.out.println("Double between 5.0 and 10.00: RandomDoubleNumber = "+getRandomDoubleBetweenRange(10.0, 100.00));

}

private static double getRandomDoubleBetweenRange(double min, double max) {

double x = (Math.random()*((max-min)+1))+min;

    return x;

}

}

Output of this code is: Double between 5.0 and 10.00: RandomDoubleNumber = 68.11912269349051

Hope this helps!

Join our Java online course and learn more about Java.

Thanks!

answered Apr 13, 2018 by Akrati
• 3,190 points
0 votes

You can achieve that concisely in Java:

Random random = new Random();

int maximum = 10;
int minimum = 5;
int totalNumber = 10;

IntStream stream = random.ints(totalNumber, minimum, maximum);
stream.forEach(System.out::println);
answered Jul 25, 2018 by samarth295
• 2,220 points

Related Questions In Java

0 votes
1 answer

How do I generate random integers within a specific range in Java?

Before Java 1.7, the standard way to ...READ MORE

answered Dec 22, 2020 in Java by Gitika
• 65,770 points
807 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 81,222 views
0 votes
2 answers

How to round any number to n decimal places in Java?

new BigDecimal(String.valueOf(double)).setScale(yourScale, BigDecimal.ROUND_HALF_UP); will get you a BigDecimal. To ...READ MORE

answered Aug 26, 2019 in Java by Sirajul
• 59,230 points
1,689 views
0 votes
3 answers

How to read a Text File in Java?

You can use readAllLines and the join method to ...READ MORE

answered Jul 28, 2018 in Java by samarth295
• 2,220 points
2,489 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
2,310 views
0 votes
5 answers

How to compare Strings in Java?

String fooString1 = new String("foo"); String fooString2 = ...READ MORE

answered Jul 12, 2018 in Java by Daisy
• 8,140 points
2,548 views
0 votes
1 answer

How to divide a string in two parts

String s="yourstring"; boolean flag = true; for(int i=0;i<s.length();i++) { ...READ MORE

answered Apr 13, 2018 in Java by Rishabh
• 3,620 points
1,190 views
0 votes
2 answers

Integer to String conversion in java

We can do this in 2 ways: String ...READ MORE

answered Jul 28, 2018 in Java by samarth295
• 2,220 points
1,188 views
0 votes
2 answers

How to create a 2-D array in java?

int[][] multi = new int[5][]; multi[0] = new ...READ MORE

answered Jul 16, 2018 in Java by Daisy
• 8,140 points
1,543 views
0 votes
2 answers

How to convert array into list in Java?

Another workaround if you use apache commons-lang: int[] ...READ MORE

answered Aug 10, 2018 in Java by samarth295
• 2,220 points
888 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP