Random number is an arbitrary number with in the defined range .For example if we define our range between 1 to 100 , then random number helps to generate any value between 1 and 100.
It can be handy tool in java as many people requires to generate random numbers to make the things or games look interesting to the audience and also helps to stop the application being hard coded .Otherwise it would have perform in the same manner .
Things to learn here in this code
array[d] = r.nextInt(32768) ;
//here r is a random class object , and nextInt is a method of random class , so above line point out that
// the value return by nextInt() function will be any between 1 and 32768
Random is the built in class in java which is found in java.util package .
There are many methods in Random class in java which one can use frequently in the java program
Some mostly used functions are :
Function Name Return Type Description
next(int bits) int Generates the next pseudorandom number .
nextBoolean() boolean Returns the next pseudorandom uniformly distributed
boolean value from this random number generators seq.
nextDouble() double It returns the pseudorandom number double value
between 0.0 & 1.0
nextFloat() float It returns the pseudorandom number float value between
0.0 and 1.0
nextInt() int It returns the pseudorandom integer value
nextInt(int n) int It returns the pseudorandom integer value between 0
(included) and n(excluded)
nextLong() long It returns the pseudorandom long value.
There is also a random() method in math class which is in java.math package . Then the natural question arises is that what is the difference in the java.util.random class and java.math random method
The answer to the question is that the random class in util package can be used to generate any pseudorandom number data type , while the random method in the java.math is static , thus it always return double value . The double value will always lies between 0.0 (inclusive) and 1.0 (exclusive) .
The important thing to keep in mind while using java.util random class is that we only need one object of random class to generate as many pseudonumbers as we can . We do not need to create the Random class object each time to generate the psedonumbers .
Also , one more point to keep in mind is that the pseudorandom integer value can only be generated in java by using java.util.Random class . Only the object of Random class is capable to generate any integer value . So never use java.math class random method to generate pseudorandom integer values as it always return the double value .
Demo :
Please find the Code below
It can be handy tool in java as many people requires to generate random numbers to make the things or games look interesting to the audience and also helps to stop the application being hard coded .Otherwise it would have perform in the same manner .
Things to learn here in this code
array[d] = r.nextInt(32768) ;
//here r is a random class object , and nextInt is a method of random class , so above line point out that
// the value return by nextInt() function will be any between 1 and 32768
Random is the built in class in java which is found in java.util package .
There are many methods in Random class in java which one can use frequently in the java program
Some mostly used functions are :
Function Name Return Type Description
next(int bits) int Generates the next pseudorandom number .
nextBoolean() boolean Returns the next pseudorandom uniformly distributed
boolean value from this random number generators seq.
nextDouble() double It returns the pseudorandom number double value
between 0.0 & 1.0
nextFloat() float It returns the pseudorandom number float value between
0.0 and 1.0
nextInt() int It returns the pseudorandom integer value
nextInt(int n) int It returns the pseudorandom integer value between 0
(included) and n(excluded)
nextLong() long It returns the pseudorandom long value.
There is also a random() method in math class which is in java.math package . Then the natural question arises is that what is the difference in the java.util.random class and java.math random method
The answer to the question is that the random class in util package can be used to generate any pseudorandom number data type , while the random method in the java.math is static , thus it always return double value . The double value will always lies between 0.0 (inclusive) and 1.0 (exclusive) .
The important thing to keep in mind while using java.util random class is that we only need one object of random class to generate as many pseudonumbers as we can . We do not need to create the Random class object each time to generate the psedonumbers .
Also , one more point to keep in mind is that the pseudorandom integer value can only be generated in java by using java.util.Random class . Only the object of Random class is capable to generate any integer value . So never use java.math class random method to generate pseudorandom integer values as it always return the double value .
Demo :
Please find the Code below
import java.util.Random; public class RandomValueLoop { // generate random numbers and display the largest public static void main(String args[]){ Random r = new Random(); int array [] = new int[10]; if(array.length==0) System.out.print("not possible"); for(int d = 0; d <array.length; d++){ array[d] = r.nextInt(32768) ; } int max = array[0]; System.out.println("Random Array........"); for( int q = 0; q < array.length; q++ ){ System.out.println(array[q]); if(array[q] > max){ max = array[q]; } } System.out.println("Maximum Number is: "+ max); } }