Created
February 17, 2016 03:25
-
-
Save harshadura/680797cc27c0e005de75 to your computer and use it in GitHub Desktop.
Q) Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7. - Java solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by harshadura on 2/17/16. | |
*/ | |
public class GenerateRandom { | |
private static int[][] range7Array = new int[][]{ | |
{1, 2, 3, 4, 5}, | |
{6, 7, 1, 2, 3}, | |
{4, 5, 6, 7, 1}, | |
{2, 3, 4, 5, 6}, | |
{7, 4, 2, 3, 1} | |
}; | |
public static void main(String args[]) { | |
System.out.println("Random Number between 1-7: " + generateRandomNumberRange7()); | |
} | |
private static int generateRandomNumberRange7() { | |
int result = 0; | |
int i = generateRandomNumberRange5(); | |
int j = generateRandomNumberRange5(); | |
result = range7Array[i - 1][j - 1]; | |
return result; | |
} | |
private static int generateRandomNumberRange5() { | |
int min = 1; | |
int max = 5; | |
int randomNum = min + (int)(Math.random() * max); | |
System.out.println("generateRandomNumberRange5: " + randomNum); | |
return randomNum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment