-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImmediate Programming
More file actions
37 lines (32 loc) · 1.22 KB
/
Immediate Programming
File metadata and controls
37 lines (32 loc) · 1.22 KB
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
28
29
30
31
32
33
34
35
36
37
import java.util.*;
public class Main {
public static void main(String[] args) {
int times = 5, sides = 6, dice = 2; // initialize variables
int[] rolls = new int[times], occurrences = new int[dice * sides];
for (int i = 0; i < times; i++) { // perform the rolls and update tallies
for (int j = 0; j < dice; j++)
rolls[i] += roll(1, sides);
occurrences[rolls[i]-1]++;
}
for (int i = 0; i < occurrences.length; i++) // print histogram of results
System.out.printf("%2s %-20s%n", i+1, barify(occurrences[i]));
}
public static int randomIndex(int lengthOfArray){
Random rand = new Random();
return rand.nextInt(lengthOfArray);
}
public static int roll(int min, int max) {
int range = max - min;
return (int)(Math.random() * range + 1) + min;
}
public int randomint (int min, int max){
Random rd = new Random();
return rd.nextInt(min, max + 1);
}
public static String barify(int value) {
StringBuilder bar = new StringBuilder();
for (int i = 0; i < value; i++)
bar.append('*');
return bar.toString();
}
}