dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Jul 1, 2021

Generate Random Numbers in Java Within a Range

In this Java programming tutorial we are going to learn how to generate random numbers within a provided number range while also setting a limit. For this example, we set the range between 10 and 25 and generate 5 random numbers. All of this is accomplished using Java’s java.util.Random.

*/

import java.util.Random;

public class LimitsEx {

    public static void main(String[] args)
	{
		LimitsEx limitsEx = new LimitsEx();
		limitsEx.proceed();
	}

	private void proceed()
	{
		Random random = new Random();
		random.ints(10,25).limit(5).sorted().forEach(System.out::println);
		
	}
}

/*

This example Java code outputs the following random numbers:

Output:
$java LimitsEx
11
15
17
22
22
Sridhar M S
 
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date