-
-
Notifications
You must be signed in to change notification settings - Fork 417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add code snippet for Generate random numbers #116
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition to the review comments, there is a merge conflict that needs to be resolved.
* @param y right bound | ||
* @return random integer between x and y | ||
*/ | ||
public static int generateRandomNumberBetweenXAndY(int x, int y) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest generateRandomNumberBetween
(leave out the end)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
/** | ||
* Throw dice (3d6) | ||
* | ||
* @return random integer between 1 and 6 (inclusive) | ||
*/ | ||
public static int throwDice() { | ||
return random.nextInt(5) + 1; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe the function could take parameters of how many dice we have and what kind of dice it is. For example, parameters 3 and 6 would throw 3 times d6 dice giving a result between 3-18.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@Test | ||
void testGenerateRandomNumberBetweenXAndY() { | ||
var randomInteger0 = GenerateRandomNumbersSnippet.generateRandomNumberBetweenXAndY(0, 0); | ||
assertEquals(0, randomInteger0); | ||
|
||
var randomInteger1 = GenerateRandomNumbersSnippet.generateRandomNumberBetweenXAndY(3, 10); | ||
var listOfPossibleResults1 = List.of(3, 4, 5, 6, 7, 8, 9, 10); | ||
assertTrue(listOfPossibleResults1.contains(randomInteger1)); | ||
|
||
var randomInteger2 = GenerateRandomNumbersSnippet.generateRandomNumberBetweenXAndY(42, 43); | ||
var listOfPossibleResults2 = List.of(42, 43); | ||
assertTrue(listOfPossibleResults2.contains(randomInteger2)); | ||
} | ||
|
||
/** | ||
* Tests for {@link GenerateRandomNumbersSnippet#throwDice()}. | ||
*/ | ||
@Test | ||
void testThrowDice() { | ||
var number = GenerateRandomNumbersSnippet.throwDice(); | ||
var listOfPossibleResults = List.of(1, 2, 3, 4, 5, 6); | ||
assertTrue(listOfPossibleResults.contains(number)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think parameterized tests would be very useful here to test more thoroughly. See https://www.baeldung.com/parameterized-tests-junit-5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parameterized tests added. I'm new to junit, thanks for the opportunity to learn.
15cc998
to
a01d1b1
Compare
Ready for review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to fix the failing build
I had some trouble working with checkstyle.xml It seems to be OK by now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The build is still failing due to Checkstyle issue. Please set up your IDE to work with Checkstyle and run the checks locally before submitting changes for review.
Sorry for the delay. Hope this last commit has fixed the problem with the checkstyle. I'm new to gradle. The project still do not build on my device but the reason is build.gradle file. I'm not sure if I should fix it along with this issue. Let me know if it's ok. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The build is still failing due to the missing license header and Checkstyle issues.
Ok, @asilenko I checked your code very fast and in my Idea everything compile successfully... almost. In class GenerateRandomNumbersSnippetTest change line 67. Line is longer than 100 characters. In checkstyle.xml at line 34 the max line length value equals 100. Line 67 has 102. |
```java | ||
public static int calculateLuhnChecksum(long num) { | ||
public static int calculateLuhnChecksum(long num) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to modify this line
private static final Random random = new Random(); | ||
|
||
public static int generateRandomNumberBetween(int x, int y) { | ||
int exclusiveRightBound = y - x + 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use var
keyword where possible
@Test | ||
void checkThatAllNumbersInTheSpecifiedRangeHaveBeenGenerated() { | ||
List<Integer> listOfResults = new ArrayList<Integer>(); | ||
for (int i = 0; i < 200; i++) { | ||
listOfResults.add(GenerateRandomNumbersSnippet.generateRandomNumberBetween(1, 10)); | ||
} | ||
List<Integer> listOfDistinctResults = | ||
listOfResults.stream() | ||
.distinct() | ||
.sorted(Integer::compareTo) | ||
.collect(Collectors.toList()); | ||
|
||
List<Integer> expectedResult = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); | ||
|
||
assertEquals(listOfDistinctResults, expectedResult); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test looks like it can randomly fail. I'm not sure I understand the purpose of the test either.
* | ||
* @param numberOfDice represents number of dice being thrown | ||
* @param typeOfDice represents number of sides of the dice | ||
* @return random integer between 1 and 6 (inclusive) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not true, depends on the parameters
@CsvSource({ | ||
"1, 6", | ||
"2, 6", | ||
"3, 6", | ||
"1, 10", | ||
"2, 10", | ||
"10, 6", | ||
"10, 10", | ||
"20, 5" | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test data is lacking 0 and negative numbers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
Closed due to inactivity. Thank you for your contributions. |
Added code snippet for Issue #105 :
-> for random number between x and y
-> for throwing dice