How to get a random number from a range in a dart?

How to get a random number in a range close to C # Random.Next (int min, int max);

+11
source share
3 answers
import 'dart:math'; final _random = new Random(); /** * Generates a positive random integer uniformly distributed on the range * from [min], inclusive, to [max], exclusive. */ int next(int min, int max) => min + _random.nextInt(max - min); 
+18
source

The range can be found using a simple formula as follows

 Random rnd; int min = 5; int max = 10; rnd = new Random(); r = min + rnd.nextInt(max - min); print("$r is in the range of $min and $max"); 
+6
source

An easier way to do this is to use the nextInt method inside Random:

 // Random 50 to 100: int min = 50; int max = 100; int selection = min + (Random(1).nextInt(max-min)); 

https://api.dartlang.org/stable/2.0.0/dart-math/Random-class.html

-1
source

Source: https://habr.com/ru/post/1445171/


All Articles