forked from google/codeu_coding_assessment_2017
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiceProb.java
More file actions
27 lines (27 loc) · 698 Bytes
/
DiceProb.java
File metadata and controls
27 lines (27 loc) · 698 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.*;
public class DiceProb{
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
System.out.print("Desired dice sum: ");
double num= s.nextInt();
System.out.print("Number of Rolls: ");
double rolls= s.nextInt();
dice(num, rolls);
}
public static void dice(double num, double rolls){
double sum=0;
double success=0;
for (double i=1; i<=rolls; i++) {
Random generator= new Random();
double x= generator.nextInt(6)+1;
double y=generator.nextInt(6)+1;
sum=x+y;
if (sum==num) {
success+=1;
}
}
double prob= success/rolls;
System.out.printf("Output of the probability = %.4f", prob);
System.out.println();
}
}