96

How does one get a random number within a range similar to c# Random.Next(int min, int max);

adam-singer
  • 4,489
  • 4
  • 21
  • 25

11 Answers11

102
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);
Alexandre Ardhuin
  • 71,959
  • 15
  • 151
  • 132
45

Range can be found with 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");
valerybodak
  • 4,195
  • 2
  • 42
  • 53
adam-singer
  • 4,489
  • 4
  • 21
  • 25
42

You can achieve it via Random class object random.nextInt(max) . The nextInt() method requires a max limit. The random number starts from 0 and the max limit itself is exclusive.

import 'dart:math';
Random random = new Random();
int randomNumber = random.nextInt(100); // from 0 upto 99 included

If you want to add the min limit, add the min limit to the result

int randomNumber = random.nextInt(90) + 10; // from 10 upto 99 included
thisiskelvin
  • 4,136
  • 1
  • 10
  • 17
Samir Rahimy
  • 2,580
  • 1
  • 18
  • 10
  • adding any number to the `Random().nextInt(max)` also increase the random number by that. So it doesn't fall under the range. For example, if your `max = 20` and you want a range of `15 to 20` it won't happen. However you will get `range of 15 to 35`. because of + 15 at random number. – Dhananjay Gavali Mar 21 '23 at 17:58
  • 1
    @DhananjayGavali you don't add the 10 or 20 to the max value. The trick is here, you want a number in the range of `min = 10` to `max = 100` get your random number as `c = max - min` which happens to be 90.`randomNumber = random.nextInt(90)` then add the min value to your random value as `randomNumber = random.next(90) + 10` – Samir Rahimy Mar 23 '23 at 19:34
27

This is really late, but this for anyone who still has the question.

The easiest way to get a random number between a min and a max is the following :

import 'dart:math';

int max = 10;

int randomNumber = Random().nextInt(max) + 1;

The math module in dart has a function called nextInt. This will return an integer from 0 (including 0 ) to max - 1 ( exluding max ). I want a number 1 to 10, hence I add 1 to the nextInt result.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
Safeer Abbas
  • 393
  • 4
  • 9
  • adding any number to the `Random().nextInt(max)` also increase the random number by that. So it doesn't fall under the range. For example, if your `max = 20` and you want a range of `15 to 20` it won't happen. However you will get `range of 15 to 35`. because of + 15 at random number. – Dhananjay Gavali Mar 21 '23 at 17:58
6

Generates a random integer uniformly distributed in the range from [min] to [max], both inclusive.

int nextInt(int min, int max) => min + _random.nextInt((max + 1) - min);
0xPixelfrost
  • 10,244
  • 5
  • 39
  • 58
4

It can be achieved exactly as you intended by creating extension on int to get random int value. For example:

import 'dart:math';

import 'package:flutter/foundation.dart';

extension RandomInt on int {
  static int generate({int min = 0, @required int max}) {
    final _random = Random();
    return min + _random.nextInt(max - min);
  }
}

And you can use this in your code like so:

List<int> rands = [];
for (int j = 0; j < 19; j++) {
  rands.add(RandomInt.generate(max: 50));
}

Note that static extension methods can't be called on type itself (e.g. int.generate(min:10, max:20)), but instead you have to use extension name itself, in this example RandomInt. For detailed discussion, read here.

Despotovic
  • 1,807
  • 2
  • 20
  • 24
4

To Generate a random positive integer between a given range:

final _random = new Random();

// from MIN(inclusive), to MAX(exclusive).
int randomBetween(int min, int max) => min + _random.nextInt(max - min);

// from MIN(inclusive), to MAX(inclusive).
int randomBetween(int min, int max) => min + _random.nextInt((max+1) - min);

// from MIN(exclusive), to MAX(exclusive).
int randomBetween(int min, int max) => (min+1) + _random.nextInt(max - (min+1));
Deven
  • 3,078
  • 1
  • 32
  • 34
2
import 'dart:math';

Random rnd = new Random();
// Define min and max value
int min = 1, max = 10;
//Getting range
int num = min + rnd.nextInt(max - min);
print("$num is in the range of $min and $max");
elopezp
  • 617
  • 7
  • 7
  • Please don't post only code as an answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually of higher quality, and are more likely to attract upvotes. – Alexandre B. Apr 29 '20 at 09:09
2

To generate a random double within a range, multiply a random int with a random double.

import 'dart:math';
Random random = new Random();
int min = 1, max = 10;
double num = (min + random.nextInt(max - min)) * random.nextDouble();
nilobarp
  • 3,806
  • 2
  • 29
  • 37
1

When I was making a Tetris game, I had to rotate the x-axis. For this, I wrote the following codes. I hope it will be useful for you too:

Random rnd = Random();
int min = 0;
int max = 23;
class Block {
  int x = min + rnd.nextInt(max - min);
}
smebes
  • 241
  • 3
  • 2
-1

A simpler way of doing this is to use the nextInt method within 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

scottstoll2017
  • 1,077
  • 10
  • 14
  • 14
    Downvoted for several reasons. Firstly, this answer is fundamentally no different to the highest voted answer from 6 years ago. Secondly, you are seeding the random generated with `1` every time. This means your code example will always produce the same result! – Duncan Jones Dec 24 '18 at 07:21