Skip to content

Commit

Permalink
publish
Browse files Browse the repository at this point in the history
  • Loading branch information
rtmigo committed Mar 13, 2022
1 parent 6730074 commit 704bc1e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 0.7.1

- fixed: `nextInt` results for `max >= 0x80000000` were not uniformly distributed
- narrowed the range of possible `max` values for `Drandom`

# 0.7.0+1

Expand Down
20 changes: 10 additions & 10 deletions lib/src/80_drandom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ class Drandom extends Xoshiro128pp {
///
/// This implementation is slightly faster than the standard one for
/// all [max] values, except for [max], which are powers of two.
// @override
// int nextInt(int max) {
// if (max < 1 || max > 0x7FFFFFFF) {
// throw RangeError.range(max, 1, 0x7FFFFFFF);
// }
// int r = nextRaw32();
// int m = max - 1;
// for (int u = r; u - (r = u % max) + m < 0; u = nextRaw32()) {}
// return r;
// }
@override
int nextInt(int max) {
if (max < 1 || max > 0x80000000) {
throw RangeError.range(max, 1, 0x80000000);
}
int r = nextRaw32();
int m = max - 1;
for (int u = r; u - (r = u % max) + m < 0; u = nextRaw32()) {}
return r;
}

}
7 changes: 7 additions & 0 deletions test/drandom_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ void main() {
[1686059242, 361797217, 1133571596, 465717623, 1522544346] );
});

test('Drandom nextInt range', () {
final random = Drandom();

expect(()=>random.nextInt(0x80000000), throwsRangeError); // no problem
random.nextInt(0x80000001); // problem
});

}

0 comments on commit 704bc1e

Please sign in to comment.