public class SumOfTwoDice {
public static void main(String[] args) {
int SIDES = 6;
int a = 1 + (int) (Math.random() * SIDES);
int b = 1 + (int) (Math.random() * SIDES);
int sum = a + b;
System.out.println(sum);
}
}
Here is the above code to find out the sum of two random integers between 1 and 6 or any given number.
The below is my own written code, Is this fine. The way i am achieving the Sum of Two random integers. Is this Correct ???
public class TestSample {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]); // 1
int b = Integer.parseInt(args[1]); // 6
double ran = Math.random();
System.out.println("Random Number" + ran);
double random;
if(a < b)
random = (b-a)*ran + a;
else
random = (a-b)*ran + b;
double sum = random + random;
System.out.println("Random Number" +(int)sum);
}
}
You have to use Math.random() again to generate a new random number between 0 and 1. Should be something like this: