Java Random nextInt() MethodThe nextInt() method of Random class returns the next pseudorandom, uniformly distributed int value from the random number generator's sequence. SyntaxParametersNA Return ValueThe nextInt() method returns the next pseudorandom int value drawn from the random number generator's sequence. Example 1Test it NowOutput: Random Integer value : -298063488 Random Integer value : 1400961289 Java Random nextInt() MethodThe nextInt(int n) method of Random class returns a pseudorandom int value between zero (inclusive ) and the specified value(exclusive), drawn from the random number generator?s sequence. SyntaxParametersn: It is the bound on the random number to be returned. It must be positive. Return ValueThe nextInt() method returns the next pseudorandom int value between zero and n drawn from the random number generator's sequence. ThrowsThe nextInt() method throws IllegalArgumentException, if n is not positive. Example 2Test it NowOutput: 1. Random intvalue : 0 2. Random intvalue : 1 3. Random intvalue : 0 4. Random intvalue : 3 5. Random intvalue : 1 6. Random intvalue : 0 7. Random intvalue : 0 Example 3Test it NowOutput: Enter number of players.3 Enter name of 3 Players. 1 Sukla 2 Mukta 3 Amar 1. Sukla will have 0 bonus points. 2. Mukta will have 1 bonus points. 3. Amar will have 2 bonus points. Example 4Test it NowOutput: Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive atjava.util.Random.nextInt(Random.java:388) at com.javaTpoint.JavaRandomNextIntExample4.main(JavaRandomNextIntExample4.java:9) Note: The bound value must be positive else it will give IllegalArgumentException.
Next TopicJava Random
|