How to generate a random integer between min and max in Dart

2 Answers

0 votes
import 'dart:math';

void main() {
  
    Random rnd = new Random();
  
    int min = 7, max = 15;
  
    for (int i = 0; i < 30; i++) {
        print(min + rnd.nextInt(max - min + 1));
    }
}




/*
run:

11
9
7
10
15
14
11
10
14
14
12
7
11
15
8
10
11
13
13
7
15
9
12
7
8
7
14
10
15
8

*/

 



answered Oct 19, 2022 by avibootz
0 votes
import 'dart:math';

void main() {
  
    var now = new DateTime.now();
    Random rnd = new Random(now.millisecondsSinceEpoch);
  
    int min = 7, max = 15;
  
    for (int i = 0; i < 30; i++) {
        print(min + rnd.nextInt(max - min + 1));
    }
}




/*
run:

8
15
9
14
12
7
7
7
9
13
11
7
11
7
10
7
13
9
12
15
9
12
7
9
14
9
11
10
8
7

*/

 



answered Oct 19, 2022 by avibootz

Related questions

2 answers 403 views
2 answers 299 views
299 views asked Oct 19, 2022 by avibootz
4 answers 1,891 views
1 answer 312 views
1 answer 260 views
2 answers 357 views
357 views asked Oct 19, 2022 by avibootz
...