0% found this document useful (0 votes)
34 views3 pages

Aargs: "Generating Random Integers in The Range 1..10."

This Java code defines a RandomRange class that generates random integers within a specified range. It contains a main method that logs generating 10 random integers between 1 and 10 by calling the showRandomInteger method. This method throws an error if the start is greater than the end, calculates the range, and returns a random integer within that range by generating a random fraction of the range and adding it to the start.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views3 pages

Aargs: "Generating Random Integers in The Range 1..10."

This Java code defines a RandomRange class that generates random integers within a specified range. It contains a main method that logs generating 10 random integers between 1 and 10 by calling the showRandomInteger method. This method throws an error if the start is greater than the end, calculates the range, and returns a random integer within that range by generating a random fraction of the range and adding it to the start.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

9)

import java.util.Random;

/** Generate random integers in a certain range. */


public final class RandomRange {

public static final void main(String... aArgs){


log("Generating random integers in the range 1..10.");

int START = 1;
int END = 10;
Random random = new Random();
for (int idx = 1; idx <= 10; ++idx){
showRandomInteger(START, END, random);
}

log("Done.");
}

private static void showRandomInteger(int aStart, int aEnd, Random aRandom){


if ( aStart > aEnd ) {
throw new IllegalArgumentException("Start cannot exceed End.");
}
//get the range, casting to long to avoid overflow problems
long range = (long)aEnd - (long)aStart + 1;
// compute a fraction of the range, 0 <= frac < range
long fraction = (long)(range * aRandom.nextDouble());
int randomNumber = (int)(fraction + aStart);
log("Generated : " + randomNumber);
}

private static void log(String aMessage){


System.out.println(aMessage);
}
}

screenshot
10)
public class MainClass
{
public static void main(String[] args)
{
Random random = new Random();
//Generating random integers using Random class
for(int i = 0; i < 5; i++)
{
System.out.println("Random Integers : "+random.nextInt());
}
System.out.println("-----------------------------");
//Generating random doubles using Random class
for(int i = 0; i < 5; i++)
{
System.out.println("Random Doubles : "+random.nextDouble());
}
System.out.println("-----------------------------");
//Generating random booleans using Random class
for(int i = 0; i < 5; i++)
{
System.out.println("Random booleans : "+random.nextBoolean());
}
}
}
Screenshot

You might also like