In: Computer Science
Write a function called randFill that fills the entries of an array with random integers in the range from 10 to 99 (inclusive). (You should use a standard Java method to generate the values. Your solution should use no more than 6 lines of code.) For example, a program that uses the function randFill follows.
public class P4 {
public static void main(String args[]) {
int x[]; x = randFill(5);
for (int i = 0; i < 5; i++)
System.out.print(x[i] + " "); // prints 5 random numbers
System.out.println(); // such as 93 73 12 69 40
}
}
-------------------- I Used Online Java Compiler, Java Language, Console Application--------------------
--------------------Six Output Screenshots--------------------
--------------------CODE--------------------
import java.util.Random;
public class P4 {
static int[] randFill(int limit){
int[] numbers = new int[limit];
Random random = new Random();//Create instance of Random class
called random
for (int i=0;i<limit;i++ )
numbers[i] = random.nextInt((99 - 10) + 1) + 10;
//rand.nextInt((Max - Min) + 1) + Max;
return numbers; // return array of integers contains random numbers
between 10 and 99(inclusive)
}
public static void main(String args[]) {
int x[]; x = randFill(5);
for (int i = 0; i < 5; i++)
System.out.print(x[i] + " "); // prints 5 random numbers
System.out.println(); // such as 93 73 12 69 40
}
}