JAVA CHALLENGE ZYBOOKs ACTIVITY 2.18.3: Fixed range of random numbers. Type two
ID: 3879380 • Letter: J
Question
JAVA CHALLENGE ZYBOOKs
ACTIVITY
2.18.3: Fixed range of random numbers.
Type two statements that use nextInt() to print 2 random integers between (and including) 100 and 149. End with a newline. Ex:
My Previous Incorrect Attempt :
import java.util.Scanner;
import java.util.Random;
public class RandomGenerateNumbers {
public static void main (String [] args) {
Random randGen = new Random();
int seedVal;
seedVal = 4;
randGen.setSeed(seedVal);
/* Your solution goes here */
int first = randGen.nextInt(10);
int second = randGen.nextInt(10);
System.out.println(first*seedVal*14);
System.out.println(second*seedVal*(51/4)+6);
}
}
MUST BE USED CODE TEMPLATE:
import java.util.Scanner;
import java.util.Random;
public class RandomGenerateNumbers {
public static void main (String [] args) {
Random randGen = new Random();
int seedVal;
seedVal = 4;
randGen.setSeed(seedVal);
/* Your solution goes here */
}
}
Explanation / Answer
randGen.nextInt(N) - generates the random numbers from 0 to N-1
so randGen.nextInt(50) will generate number between 0 - 49
randGen.nextInt(50) + 100 => (0 - 49) + 100 = (100 - 149)
This will range will include 149 also if you don't want to include use 49 instead of 50.
JAVA Code :
import java.util.Scanner;
import java.util.Random;
public class RandomGenerateNumbers {
public static void main (String [] args) {
Random randGen = new Random();
int seedVal;
seedVal = 4;
randGen.setSeed(seedVal);
/* Your solution goes here */
int first = randGen.nextInt(50) + 100;
int second = randGen.nextInt(50) + 100;
System.out.println(first);
System.out.println(second);
}
}
Output Screenshot:
112
102
Note:- Please comment if you have any concern.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.