forked from rabinovichr/Project02
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dice.java
31 lines (26 loc) · 823 Bytes
/
Dice.java
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
package Project02;
import java.util.Random;
/**
* The dice class has a single static roll() method that returns an int value between 0 and the number of
* sides based on a pseudo random number generator.
*/
public class Dice {
/**
*
* @param sides the number of sides on the die
* @return random value from 0 to number of sides
*/
public static int roll(int sides) {
int val;
Random random = new Random(System.currentTimeMillis());
//sides is positive
if(sides >= 0){ val = random.nextInt(sides + 1); }
//if sides is a negative value, calculate return value using positive value
// and return correct negative value
else{
val = random.nextInt(-sides + 1);
return -val;
}
return val;
}
}