Skip to content

Commit 452b09e

Browse files
authored
Update tutorial_32.java
Added option for random decimals
1 parent 53552a5 commit 452b09e

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

code/tutorial_32.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ else if (flipCoin == 2) {
4747
System.out.println("Tails!");
4848
}
4949

50+
double decimalTest = rndRange(1, 5, 4);
51+
System.out.println("Generated between 1 to 5 with 4 decimals is " + decimalTest);
52+
5053
}
5154

5255
public static int rndRange(int start, int finish) {
@@ -58,5 +61,12 @@ public static int rndRange(int start, int finish) {
5861
public static int rndRange(int finish) {
5962
return rndRange(1, finish);
6063
}
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+
}
6171

6272
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy