Skip to content

Commit

Permalink
iluwatar#105 Generate Random Number between x and y and Throw Dice
Browse files Browse the repository at this point in the history
  • Loading branch information
Shanty97 committed Feb 10, 2024
1 parent 1b587b7 commit f3a57cc
Show file tree
Hide file tree
Showing 5 changed files with 341 additions and 0 deletions.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,62 @@ public static double calculateMatchRating(double firstPlayerRating,double second
return newRating;
}
```
### Random Number Between Two Numbers

```java

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;

}

```

### Dice Throw Sum (eg. 3d6)

```java

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;
}

}

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;

}

```

## Media

Expand Down
81 changes: 81 additions & 0 deletions src/main/java/math/DiceThrow.java
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;

}

}
60 changes: 60 additions & 0 deletions src/main/java/math/RandomNumber.java
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;

}

}
53 changes: 53 additions & 0 deletions src/test/java/math/DiceThrowTest.java
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);

}
}
91 changes: 91 additions & 0 deletions src/test/java/math/RandomNumberTest.java
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));
}
}

0 comments on commit f3a57cc

Please sign in to comment.