-
-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Java random numbers implementation (#202)
* #105 Generate Random Number between x and y and Throw Dice * #105 Updated Random Number generation code and its unit test * #105 Modified the Readme file * #105 Updated code for as per CheckStyle * #105 Updates as per Sonar checks * #105 Sonarqube code resolution * #105 Linked the unit tests to the main code * #105 Sonarqube issues resolved
- Loading branch information
Showing
5 changed files
with
337 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,78 @@ | ||
/* | ||
* 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 { | ||
|
||
private static Random random = new Random(); | ||
|
||
/** | ||
* 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 diSides; | ||
|
||
DiceSides(int diceSides) { | ||
this.diSides = diceSides; | ||
} | ||
|
||
/** | ||
* Returns the number of sides of a dice. | ||
* | ||
* @return int denoting number of sides of a dice | ||
*/ | ||
public int getDiceSides() { | ||
return this.diSides; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Returns the sum of sides for the given number of sides of each dice. | ||
* | ||
* @param noOfDice number of dice | ||
* @param sides sides of a dice | ||
* @return int sum of sides for number of dice | ||
*/ | ||
public static int throwDice(int noOfDice, DiceSides sides) { | ||
|
||
int sum = 0; | ||
for (int i = 0; i < noOfDice; i++) { | ||
sum = sum + (1 + random.nextInt(sides.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,72 @@ | ||
/* | ||
* 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 { | ||
|
||
private RandomNumber() {} | ||
|
||
private static Random random = new Random(); | ||
|
||
/** | ||
* Return a random number between two given numbers. | ||
* | ||
* @param start Starting point to find the random number | ||
* @param end Ending point to find the random number | ||
* @return Number denoting the random number generated | ||
*/ | ||
public static <T extends Number> Number getRandomNumber(T start, T end) { | ||
|
||
if ((start instanceof Byte && end instanceof Byte)) { | ||
return (byte) (start.byteValue() + random.nextInt(end.byteValue() - start.byteValue() + 1)); | ||
} else if ((start instanceof Byte && end instanceof Byte) | ||
|| (start instanceof Short && end instanceof Short)) { | ||
return (short) (start.shortValue() | ||
+ random.nextInt(end.shortValue() - start.shortValue() + 1)); | ||
} else if ((start instanceof Integer && end instanceof Integer)) { | ||
return (int) (start.intValue() | ||
+ random.nextInt(end.intValue() - start.intValue() + 1)); | ||
} else if (start instanceof Long && end instanceof Long) { | ||
return (long) (start.longValue() | ||
+ random.nextLong(end.longValue() - start.longValue() + 1)); | ||
} else if (start instanceof Float && end instanceof Float) { | ||
return (float) (start.floatValue() | ||
+ random.nextFloat(end.floatValue() - start.floatValue() + 1)); | ||
} else if (start instanceof Double && end instanceof Double) { | ||
return (double) (start.doubleValue() | ||
+ random.nextDouble(end.doubleValue() - start.doubleValue() + 1)); | ||
} else { | ||
throw new IllegalArgumentException("Invalid Numbers As Arguments " | ||
+ start.getClass() + " and " + end.getClass()); | ||
} | ||
} | ||
|
||
} |
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.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import math.DiceThrow.DiceSides; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Tests for 30 Seconds of Java code library. | ||
*/ | ||
class DiceThrowTest { | ||
|
||
/** | ||
* Tests for {@link DiceThrow #throwDice(int, DiceSides)}. | ||
*/ | ||
@Test | ||
void testThrowDice() { | ||
// 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); | ||
assertEquals(0, DiceThrow.throwDice(0, DiceSides.FOUR)); | ||
|
||
} | ||
} |
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,72 @@ | ||
/* | ||
* 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.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Tests for 30 Seconds of Java code library. | ||
*/ | ||
class RandomNumberTest { | ||
|
||
/** | ||
* Tests for {@link RandomNumber #getRandomNumber(T, T)}. | ||
*/ | ||
@Test | ||
void testGetRandomNumber() { | ||
// 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 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 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 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 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)); | ||
// Test for Double range | ||
double d1 = Double.valueOf((double) 100.12); | ||
int d2 = Integer.valueOf((int) 200); | ||
assertThrows(IllegalArgumentException.class, | ||
() -> RandomNumber.getRandomNumber(d1, d2)); | ||
} | ||
} |