Random numbers
- 100% developed as of Dec 31, 2012 Statements
- 100% developed as of Mar 10, 2013 Conditional blocks
- 100% developed as of Mar 10, 2013 Loop blocks
- 100% developed as of May 24, 2013 Boolean expressions
- 100% developed as of Feb 16, 2010 Variables
- 100% developed as of Mar 10, 2013 Primitive Types
- 100% developed as of Mar 10, 2013 Arithmetic expressions
- 100% developed as of May 24, 2013 Literals
- 100% developed as of Mar 10, 2013 Methods
- 100% developed as of May 24, 2013 String
- 100% developed as of Mar 10, 2013 Objects
- 100% developed as of Jul 5, 2012 Packages
- 100% developed as of Mar 10, 2013 Arrays
- 75% developed as of Jan 11, 2013 Mathematical functions
- 75% developed as of Jan 11, 2013 Large numbers
- 75% developed as of Jan 11, 2013 Random numbers
- 100% developed as of Apr 8, 2013 Unicode
- 100% developed as of Apr 8, 2013 Comments
- 100% developed as of Sep 27, 2007 Keywords
- 100% developed as of Aug 6, 2013 Coding conventions
- 0% developed as of Mar 26, 2018 Lambda expressions
To generate random numbers the Math.random()
method can be used, which returns a double
, greater than or equal to 0.0 and less than 1.0.
The following code returns a random integer between n and m (where n <= randomNumber < m):
intrandomNumber=n+(int)(Math.random()*(m-n));
Alternatively, the java.util.Random
class provides methods for generating random boolean
s, byte
s, float
s, int
s, long
s and 'Gaussians' (double
s from a normal distribution with mean 0.0 and standard deviation 1.0). For example, the following code is equivalent to that above:
Randomrandom=newRandom(); intrandomNumber=n+random.nextInt(m-n);
As an example using random numbers, we can make a program that uses a Random object to simulate flipping a coin 20 times:
importjava.util.Random; publicclass CoinFlipper{ publicstaticvoidmain(String[]args){ // The number of times to flip the coin finalintTIMES_TO_FLIP=20; intheads=0; inttails=0; // Create a Random object Randomrandom=newRandom(); for(inti=0;i<TIMES_TO_FLIP;i++){ // 0 or 1 intresult=random.nextInt(2); if(result==1){ System.out.println("Heads"); heads++; }else{ System.out.println("Tails"); tails++; } } System.out.println("There were " +heads +" heads and " +tails +" tails"); } }
Heads Tails Tails Tails Heads Tails Heads Heads Heads Heads Heads Heads Tails Tails Tails Tails Heads Tails Tails Tails There were 9 heads and 11 tails
Of course, if you run the program you will probably get different results.
Truly random numbers
Both Math.random()
and the Random
class produce pseudorandom numbers. This is good enough for a lot of applications, but remember that it is not truly random. If you want a more secure random number generator, Java provides the java.security.SecureRandom
package. What happens with Math.random()
and the Random
class is that a 'seed' is chosen from which the pseudorandom numbers are generated. SecureRandom
increases the security to ensure that the seed which is used by the pseudorandom number generator is non-deterministic — that is, you cannot simply put the machine in the same state to get the same set of results. Once you have created a SecureRandom
instance, you can use it in the same way as you can the Random
class.
If you want truly random numbers, you can get a hardware random number generator or use a randomness generation service.
To do:
Add some exercises like the ones in Variables