Skip to main content

Generate random number in java

Java program for Generate random number . Here problem description and other solutions.

/*
  Java program for
  Print random number
*/
import java.util.Random;
public class Program
{
	// Print random number in given size
	public void simpleRandom(int size)
	{
		int number = 0;
		int i = 0;
		Random rand = new Random();
		while (i < size)
		{
			// Get new rand number
			number = rand.nextInt();
			System.out.println(number);
			i++;
		}
	}
	void randomBetweenRange(int min, int max)
	{
		// Calculate random number
		int number = min + (int)(Math.random() * ((max - min) + 1));
		System.out.println(number);
	}
	public static void main(String[] args)
	{
		Program task = new Program();
		// Test Case
		task.simpleRandom(3);
		// Range from 1 to 10
		task.randomBetweenRange(1, 10);
		// Range from 50 to 100
		task.randomBetweenRange(50, 100);
		// Range from 1000 to 2000
		task.randomBetweenRange(1000, 2000);
	}
}

Output

1289293647
1824893500
718211078
3
50
1081




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment