We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 53552a5 commit 452b09eCopy full SHA for 452b09e
code/tutorial_32.java
@@ -47,6 +47,9 @@ else if (flipCoin == 2) {
47
System.out.println("Tails!");
48
}
49
50
+ double decimalTest = rndRange(1, 5, 4);
51
+ System.out.println("Generated between 1 to 5 with 4 decimals is " + decimalTest);
52
+
53
54
55
public static int rndRange(int start, int finish) {
@@ -58,5 +61,12 @@ public static int rndRange(int start, int finish) {
58
61
public static int rndRange(int finish) {
59
62
return rndRange(1, finish);
60
63
64
65
+ public static double rndRange(int start, int finish, int decimals) {
66
+ if (decimals < 0) decimals = 0;
67
+ return (
68
+ (new Random().nextInt(finish * (int)Math.pow(10, decimals) + 1 - start) + start) / Math.pow(10, decimals)
69
+ );
70
+ }
71
72
0 commit comments