Skip to content

Commit

Permalink
iluwatar#105 Updated Random Number generation code and its unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Shanty97 committed Feb 11, 2024
1 parent f3a57cc commit 826733f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
30 changes: 18 additions & 12 deletions src/main/java/math/RandomNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,24 @@ public class RandomNumber {
*
*/
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;

Random random = new Random();

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

}

Expand Down
13 changes: 12 additions & 1 deletion src/test/java/math/RandomNumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
package math;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;



import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -64,7 +66,7 @@ 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));
assertTrue((longResult.longValue() >= -100) && (longResult.longValue() <= 2500));
}

/**
Expand All @@ -88,4 +90,13 @@ void test_doubleRandomNumber() {
assertTrue(doubleResult instanceof Double);
assertTrue((doubleResult.doubleValue() >= 100.12) && (doubleResult.doubleValue() <= 200.28));
}

/**
* Test for invalid argument numbers
*/
@Test
void test_invalidNumberArguments() {
// Test for Double range
assertThrows(IllegalArgumentException.class, () -> RandomNumber.getRandomNumber(Double.valueOf((double) 100.12), Integer.valueOf((int) 200)));
}
}

0 comments on commit 826733f

Please sign in to comment.