The document discusses the Random class in Java, which generates random numbers of various data types. It imports the Random package, creates a Random object, and uses the object's nextInt() method to generate random integers. The code example demonstrates generating a random lottery number and comparing it to a preset number, displaying different messages if they match or not. It also shows adding random numbers to an array using a for loop and printing the array values using a foreach loop.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
24 views
The Random Class in Java Is A Part of The Java
The document discusses the Random class in Java, which generates random numbers of various data types. It imports the Random package, creates a Random object, and uses the object's nextInt() method to generate random integers. The code example demonstrates generating a random lottery number and comparing it to a preset number, displaying different messages if they match or not. It also shows adding random numbers to an array using a for loop and printing the array values using a foreach loop.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
Zhila Rzgar Omer
Description: IT Stage 1 G- A
The random class in java is a part of the java.util package
That generate random numbers of int, long, float, double… etc type, it uses an algorithm based on seed value which is the starting point for the number generating process.
Import java.util.Random; → to import the Random package
Random name = new Random(); → to create an object int variableName = name.nextInt(); → to create a variable that e takes the random numbers eeee from the object Syntax:
Example with array:
Example with variable: Scanner sc = new Scanner (System.in); Random lottery = new Random();
int lotteryNum = 2310;
System.out.println("The lotter number is " + lotteryNum);
System.out.print("Click enter to generate your lottery number");
String click = sc.nextLine();
String result =""; Output example
The lottery number is 231 for(int c=0; c<4; c++){ Click enter to generate your lottery number int yourLottery = lottery.nextInt(4); your lottery number is 301 result += yourLottery; } Sorry, you lost. Please try again
System.out.println("your lottery number is " + result);
Random number = new Random(); int arr[] = new int[6]; int resultInt = Integer.parseInt(result); Output example if(resultInt == lotteryNum){ //what's the error? The numbers in this array are: for(int c=0; c<arr.length; c++){ System.out.println("Congratulations, you won the5lottery! 0 ");4 0 5 1 arr[c]= number.nextInt(6); } }else{ System.out.println("Sorry, you lost. Please try again"); }v System.out.println("The numbers in this array are: " ); for (int p : arr) { System.out.print(p The above code creates 3+ "\t"); random} numbers from 0 to 3 for a lottery and compares them to the lottery number, there will be two different messages given to the user whether the generated This program number and theadds random lottery numbernumbers into an array using a for loop and are equal. displays the numbers in array at the end using a foreach loop.