Another highly popular interview question – how do you generate random numbers 1 through 7 or 1 through 9 (or any other range) using a function that generates numbers 1-5. In this article, I will show you how to do that, with some further analysis.
To generate a random number 1- 5, I have used the following function. It is to note that Math.random() generates 0.0 through 1.0. I have used Math.ceil function to generate numbers through 1-5. Math.ceil(0.0) is 1 strangely. It’s probably because of the double representation such as 0.0 being 0.00000000001 etc.
public static int randomFive() {
return (int) (Math.ceil(Math.random() * 5));
}
To generate random number in another range, I have used called this method twice to get a good sample and to include all the numbers in the range, then taking a modulus to make sure the numbers are not out of range. In the following program, I have then called these methods randomSeven() and randomNine() one thousand times each just to see how often different numbers in the given range get generated.
Here is the full source code:
package com.icodejava.blog;
import java.util.Arrays;
/**
* @author Kushal Paudyal
* www.icodejava.com
* Created On - Mar 8, 2014
* Last Modified On - Mar 8, 2014
*
* Generates random number 1-9 and 1-7
* using function that generates random number 1-5
*/
public class RandomGenerator {
public static void main(String args[]) {
generateRandomNineAndPrint();
generateRandomSevenAndPrint();
}
/**
* This method calls randomNine() method 1000 times and updates the count of
* each number from 1-9 being generated.
*
* randomNine() method generates numbers from 1-9 using randomFive() method.
*/
private static void generateRandomNineAndPrint() {
int[] counts = new int[9];
for (int i = 0; i < 1000; i++) {
int value = randomNine();
counts[value - 1]++;
}
System.out.println("Count of Generated Random Numbers 1- 9:" + Arrays.toString(counts));
}
/**
* This method calls randomSeven() method 1000 times and updates the count
* of each number from 1-7 being generated.
*
* randomNine() method generates numbers from 1-7 using randomFive() method.
*/
private static void generateRandomSevenAndPrint() {
int[] counts = new int[7];
for (int i = 0; i < 1000; i++) {
int value = randomSeven();
counts[value - 1]++;
}
System.out.println("Count of Generated Random Numbers 1- 7:" + Arrays.toString(counts));
}
/**
* Uses randomFive() method that generates 1-5 to generate numbers from 1-9
*
* @return
*/
public static int randomNine() {
int random;
while (true) {
/**
* When you call randomFive() two times, there are a lot of possible
* combinations.25 to be exact. (1,1) (1,2) (1,3) ... (5,5)
*
* We need to call it two times and use the formula below to make
* sure we generate numbers from 1-9. Otherwise, it will not
* generate some numbers.
*/
random = (randomFive() - 1) * 3 + (randomFive() + 1) * 2;
return random % 9 + 1;
}
}
/**
* Uses randomFive() method that generates 1-5 to generate numbers from 1-7
*
* @return
*/
public static int randomSeven() {
int random;
while (true) {
/**
* When you call randomFive() two times, there are a lot of possible
* combinations.25 to be exact. (1,1) (1,2) (1,3) ... (5,5)
*
* We need to call it two times and use the formula below to make
* sure we generate numbers from 1-7. Otherwise, it will not
* generate some numbers.
*/
random = (randomFive() - 1) * 3 + (randomFive() + 1) * 2;
return random % 7 + 1;
}
}
/**
* This method generates a random number 1 through 5. It uses Math.random()
* method for randomizing.
* Note: Math.ceil(0.0) is 1
* @return
*/
public static int randomFive() {
return (int) (Math.ceil(Math.random() * 5));
}
}
Here is the output of running above program:
Count of Generated Random Numbers 1- 9:[127, 127, 63, 131, 154, 35, 153, 129, 81]
Count of Generated Random Numbers 1- 7:[114, 172, 115, 167, 162, 107, 163]