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,050 points
53 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

answered Apr 13, 2018 by Akrati
• 3,150 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,190 points

Related Questions In Java

+5 votes
3 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,380 points

edited Nov 6, 2018 by Omkar 9,261 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 in Java by Sirajul
• 31,060 points
87 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 27, 2018 in Java by samarth295
• 2,190 points
103 views
0 votes
1 answer

How to print java array in the simplest way?

String[] arr = new String[] {"John", "Mary", ...READ MORE

answered Apr 17, 2018 in Java by sophia
• 1,400 points
79 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 in Java by Sirajul
• 31,060 points
167 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,050 points
125 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,540 points
47 views
0 votes
2 answers

Integer to String conversion in java

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

answered Jul 27, 2018 in Java by samarth295
• 2,190 points
75 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,050 points
48 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 9, 2018 in Java by samarth295
• 2,190 points
51 views