-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add nextIntRange to Random #6524
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
Comments
This was briefly discussed when I created the current Random class but since it is literally "return min + rnd.nextInt(max - min);" it was deemed unnecessary. Over to the libraries team to reevaluate. Removed Type-Defect label. |
For context, the author of this patch is one of our earliest adopters and supporters. What may be trivial or unnecessary to us is very helpful to others. |
This comment was originally written by @bp74 As a C# developer i tried to use this method too and i was surprised that it's not there. Sure it's easy to work around but the code looks better with the a method like this. |
This comment was originally written by [email protected] Related feature request: add a 'choice(Iterable it)' method that would be a terser way to express it.elementAt(randInt(it.length)). Both of these methods could be implemented on Random itself as concrete methods that delegate to randInt(), instead of adding new abstract methods that would have to be implemented by everyone. |
I think all of @lrhn - would you accept a patch adding these? Or are they breaking because of |
It's a breaking change because it's an interface that is already being implemented. I don't think even Dart 3 will allow us to break interfaces without very good reason. Backwards compatibility is going to be more important in the future than it has ever been before. All the operations can be based on I recommend just putting them in a package, say package:math (I think we reserved that name, not sure if we have used it yet). Random _defaultRandom;
int randomIntInRange(int start, int end, [Random random]) {
if (start <= end) {
return (random ?? _defaultRandom = Random()).nextInt(end - start) + start;
}
}
T randomElement<T>(Iterable<T> elements, [Random random]) {
random ??= _defaultRandom = Random();
if (elements is List<Object>) {
return elements.elementAt(random.nextInt(elements.length));
}
T result = null;
int count = 0;
for (var element in elements) {
if (random.nextInt(++count) == 0) result = element;
}
return element;
}
Future<T> randomAsyncElement<T>(Stream<T> elements, [Random random]) async {
random ??= _defaultRandom = Random();
T result = null;
int count = 0;
await for (var element in elements) {
if (random.nextInt(++count) == 0) result = element;
}
return element;
} (Or, if you don't want to return |
@lrhn Your preference to add static or top-level methods here (but not Internally I can can count 31 import 'package:mockito/mockito.dart';
// OK: Since Mock implements noSuchMethod we can add new methods to Random.
class MockRandom extends Mock implements Random {} ... or: class FakeRandom implements Random {
double value;
FakeRandom(this.value);
double nextDouble() => value;
int nextInt(int max) => (value * max).toInt();
bool nextBool() => value != 0;
} If we had default methods, we could do this in a non-breaking way. Personally, I still think we should consider some way of doing class-level evolution for Dart 3.0 changes, otherwise we might as well just start |
These seem like ideal candidates for extension methods which could go in In the mean time top level methods seems like a good compromise. I've started an internal thread about getting that package going - assuming all goes well I should be able to close this issue by publishing them there. |
This issue was originally filed by @financecoding
Adding function to Random to help with getting a random number between a range (min, max). Depending on when Random gets refactored this could be a solution for now.
https://codereview.chromium.org/11367087
The text was updated successfully, but these errors were encountered: