Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

23.
Bucle: Sentencia break (3/4) Random number = new Random(); int i; i is 43 while (true) { i is 2 i = number.nextInt(100); i is 12 System.out.println("i is " + i); i is 95 if (i >= 80) break; } Random class will be discussed later. 23

27.
Random Numbers (1/2) Random number generation is discussed in section 3.8 of your textbook. We learn another way here, using the Random class. Notes:  Need to import java.util.*;  Random( ): constructs a new random number generator Random whose seed is based on the current time.  int nextInt(int n): returns the next pseudo-random, from the interval [0, 1, … n-1], uniformly distributed value of a Random object.  Refer to the API specification for the complete description of class Random. 27

28.
Random Numbers (2/2)import java.util.*;public class Random1 { public static void main(String[] args) { Random num = new Random(); for (int i=0; i<5; i++) { System.out.println("Next random number is " + num.nextInt(100)); } } Next random number is 48} Next random number is 14 Next random number is 89 Next random number is 7 Next random number is 44 28