Generating a random number within a range
You may have often wondered how to generate a random number from within a certain range. This is exactly what we will look at in this recipe; we will obtain a random number that resides in an interval between a minimum (min) and maximum (max) value.
How to do it...
This is simple; look at how it is done in random_range.dart
:
import 'dart:math'; var now = new DateTime.now(); Random rnd = new Random(); Random rnd2 = new Random(now.millisecondsSinceEpoch); void main() { int min = 13, max = 42; int r = min + rnd.nextInt(max - min); print("$r is in the range of $min and $max"); // e.g. 31 // used as a function nextInter: print("${nextInter(min, max)}"); // for example: 17 int r2 = min + rnd2.nextInt(max - min); ...
Get Dart: Scalable Application Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.