forked from iluwatar/30-seconds-of-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
iluwatar#105 Generate Random Number between x and y and Throw Dice
- Loading branch information
Showing
5 changed files
with
341 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2017-2022 Ilkka Seppälä | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package math; | ||
|
||
import java.util.Random; | ||
|
||
/** | ||
* | ||
* Sum of Dice throw | ||
* Eg. 3d6 - 3 dice having 6 sides | ||
* | ||
*/ | ||
public class DiceThrow { | ||
|
||
/** | ||
* | ||
* Enum for standardized sided dice (4,6,8,10,12 and 20) | ||
* | ||
*/ | ||
public enum DiceSides { | ||
|
||
FOUR(4), SIX(6), EIGHT(8), TEN(10), TWELVE(12), TWENTY(20); | ||
|
||
private final int diceSides; | ||
|
||
/** | ||
* | ||
* @param diceSides sides of a dice | ||
* | ||
*/ | ||
DiceSides(int diceSides) { | ||
this.diceSides = diceSides; | ||
} | ||
|
||
public int getDiceSides() { | ||
return this.diceSides; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* | ||
* @param noOfDice number of dice | ||
* @param diceSides sides of a dice | ||
* @return int sum of sides for number of dice | ||
* | ||
*/ | ||
public static int throwDice(int noOfDice, DiceSides diceSides) { | ||
|
||
int sum = 0; | ||
for (int i = 0; i < noOfDice; i++) { | ||
sum = sum + (1 + new Random().nextInt(diceSides.getDiceSides())); | ||
} | ||
|
||
return sum; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2017-2022 Ilkka Seppälä | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package math; | ||
|
||
import java.util.Random; | ||
|
||
/** | ||
* | ||
* Random Number between given two values | ||
* Supported Data types - Byte, Short, Integer, Long, Float and Double | ||
* | ||
*/ | ||
public class RandomNumber { | ||
|
||
/** | ||
* | ||
* @param start Starting point to find the random number | ||
* @param end Ending point to find the random number | ||
* @return <T extends Number> | ||
* | ||
*/ | ||
public static <T extends Number> Number getRandomNumber(T start, T end) { | ||
|
||
if ((start instanceof Byte && end instanceof Byte) || | ||
(start instanceof Short && end instanceof Short) || | ||
(start instanceof Integer && end instanceof Integer)) | ||
return (int) start.intValue() + new Random().nextInt(end.intValue() - start.intValue() + 1); | ||
else if (start instanceof Long && end instanceof Long) | ||
return (long) start.longValue() + new Random().nextLong(end.longValue() - start.longValue() + 1); | ||
else if (start instanceof Float && end instanceof Float) | ||
return (float) start.floatValue() + new Random().nextFloat(end.floatValue() - start.floatValue() + 1); | ||
else if (start instanceof Double && end instanceof Double) | ||
return (double) start.doubleValue() + new Random().nextDouble(end.doubleValue() - start.doubleValue() + 1); | ||
return end; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2017-2022 Ilkka Seppälä | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package math; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import math.DiceThrow.DiceSides; | ||
|
||
/** | ||
* Tests for 30 Seconds of Java code library. | ||
*/ | ||
public class DiceThrowTest { | ||
|
||
/** | ||
* Test for generating random short number | ||
*/ | ||
@Test | ||
void test_throwDice() { | ||
// Test for Dice throw | ||
assertTrue(DiceThrow.throwDice(3, DiceSides.SIX) <= 18); | ||
assertTrue(DiceThrow.throwDice(2, DiceSides.FOUR) <= 8); | ||
assertTrue(DiceThrow.throwDice(3, DiceSides.EIGHT) <= 24); | ||
assertTrue(DiceThrow.throwDice(4, DiceSides.TEN) <= 40); | ||
assertTrue(DiceThrow.throwDice(1, DiceSides.TWELVE) <= 12); | ||
assertTrue(DiceThrow.throwDice(2, DiceSides.TWENTY) <= 40); | ||
assertTrue(DiceThrow.throwDice(0, DiceSides.FOUR) == 0); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2017-2022 Ilkka Seppälä | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package math; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Tests for 30 Seconds of Java code library. | ||
*/ | ||
public class RandomNumberTest { | ||
|
||
/** | ||
* Test for generating random short number | ||
*/ | ||
@Test | ||
void test_shortRandomNumber() { | ||
// Test for Short range | ||
Number shortResult = RandomNumber.getRandomNumber(Short.valueOf((short) 2), Short.valueOf((short) 7)); | ||
assertTrue(shortResult instanceof Short); | ||
assertTrue((shortResult.shortValue() >= 2) && (shortResult.shortValue() <= 7)); | ||
} | ||
|
||
/** | ||
* Test for generating random integer number | ||
*/ | ||
@Test | ||
void test_intRandomNumber() { | ||
// Test for Integer range | ||
Number intResult = RandomNumber.getRandomNumber(Integer.valueOf(5), Integer.valueOf(10)); | ||
assertTrue(intResult instanceof Integer); | ||
assertTrue((intResult.intValue() >= 5) && (intResult.intValue() <= 10)); | ||
} | ||
|
||
/** | ||
* Test for generating random long number | ||
*/ | ||
@Test | ||
void test_longRandomNumber() { | ||
// Test for Long range | ||
Number longResult = RandomNumber.getRandomNumber(Long.valueOf((long) -100), Long.valueOf((long) 2500)); | ||
assertTrue(longResult instanceof Long); | ||
assertTrue((longResult.longValue() <= -100) && (longResult.longValue() <= 2500)); | ||
} | ||
|
||
/** | ||
* Test for generating random float number | ||
*/ | ||
@Test | ||
void test_floatRandomNumber() { | ||
// Test for Float range | ||
Number floatResult = RandomNumber.getRandomNumber(Float.valueOf((float) 2.5f), Float.valueOf((float) 25.4f)); | ||
assertTrue(floatResult instanceof Float); | ||
assertTrue((floatResult.floatValue() >= 2.5f) && (floatResult.floatValue() <= 25.4f)); | ||
} | ||
|
||
/** | ||
* Test for generating random double number | ||
*/ | ||
@Test | ||
void test_doubleRandomNumber() { | ||
// Test for Double range | ||
Number doubleResult = RandomNumber.getRandomNumber(Double.valueOf((double) 100.12), Double.valueOf((double) 200.28)); | ||
assertTrue(doubleResult instanceof Double); | ||
assertTrue((doubleResult.doubleValue() >= 100.12) && (doubleResult.doubleValue() <= 200.28)); | ||
} | ||
} |