Ask Question

Type two statements using nextInt () to print two random integers between (and including) 0 and 9. End with a newline. Ex: 5 7 Note: For this activity, using one statement may yield different output (due to the interpreter calling randGen. nextInt () in a different order). Use two statements for this activity. (Notes)

+3
Answers (2)
  1. 11 November, 21:54
    0
    import java. util. Random;

    public class RandomGenerator

    {

    public static void main (String[] args) {

    Random randomGenerator = new Random ();

    int seed = 0;

    randomGenerator. setSeed (seed);

    System. out. println (randomGenerator. nextInt (10));

    System. out. println (randomGenerator. nextInt (10));

    }

    }

    Explanation:

    In order to create random number using java. util. Random class, we need to create an object from the class, set a seed value, and call the appropriate function. In this case, the object is created as randomGenerator, the seed value is set to 0. To create the integers, we need to use nextInt (). This function takes one parameter to specify the range, and creates a number between 0 and given number (given number is not included). Since, it is asked to create integers between 0 and 9, we should pass 10 as a parameter.
  2. 11 November, 22:02
    0
    random1 = randGen. nextInt (10);

    random2 = randGen. nextInt (10);

    The value of 10 as a scaling factor is used because counting 0 - 9 inclusive, we have 10 possibilities.

    Explanation:

    / / Random class is imported to allow the program generate random object

    import java. util. Random;

    //The class definition

    public class Solution {

    / / main method is defined which signify beginning of program execution

    public static void main (String[ ] args) {

    / / Random object called randGen is declared

    Random randGen = new Random ();

    / / random variable 1

    int random1;

    / / random variable 2

    int random2;

    / / random1 is generated and printed

    System. out. println (random1 = randGen. nextInt (10));

    / / random2 is generated and printed

    System. out. println (random2 = randGen. nextInt (10));

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Type two statements using nextInt () to print two random integers between (and including) 0 and 9. End with a newline. Ex: 5 7 Note: For ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers