How to get a random number from range in dart?

Dart

Dart Problem Overview


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

Dart Solutions


Solution 1 - Dart

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);

Solution 2 - Dart

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");

Solution 3 - Dart

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

Solution 4 - Dart

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.

Solution 5 - Dart

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.

Solution 6 - Dart

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);

Solution 7 - Dart

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");

Solution 8 - Dart

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();

Solution 9 - Dart

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionadam-singerView Question on Stackoverflow
Solution 1 - DartAlexandre ArdhuinView Answer on Stackoverflow
Solution 2 - Dartadam-singerView Answer on Stackoverflow
Solution 3 - DartSamir RahimyView Answer on Stackoverflow
Solution 4 - DartSafeer AbbasView Answer on Stackoverflow
Solution 5 - DartDespotovicView Answer on Stackoverflow
Solution 6 - Dart0xPixelfrostView Answer on Stackoverflow
Solution 7 - DartelopezpView Answer on Stackoverflow
Solution 8 - DartnilobarpView Answer on Stackoverflow
Solution 9 - Dartscottstoll2017View Answer on Stackoverflow