How to generate random number in java
java.util.Random class is use to generate some random number in java.
Methods of Random class
Method Name | Method Working |
protected int next(int bits) | Use to generates the next pseudorandom number. |
double nextDouble() | Returns the next pseudorandom |
float nextFloat() | Returns the next pseudorandom |
int nextInt() | Returns the next pseudorandom |
int nextInt(int n) | Returns a pseudorandom |
long nextLong() | Returns the next pseudorandom. |
Example
package javaenotes; import java.util.Random; public class StudentInfo extends Thread { public static void main(String[] args) { Random random = new Random(); for (int i = 0; i < 5; i++) { int randomNumber = random.nextInt(); System.out.println("Random number: " + randomNumber); } } }
Output
Random number: 684985468 Random number: 824215667 Random number: 2099679075 Random number: -831622058 Random number: 979547511
Comments
Post a Comment