{

How to Generate Random Numbers and range of numbers in Dart or Flutter Programming with example


Generate Random Number between ranges in Dart

This tutorial shows multiple ways to Generate Random Numbers in the Dart programming language.

A random number is a unique number generated between a minimum and maximum value for multiple requests

The returned number is always between the ranges of values provided.

Dart random Number Generator example

Dart provides dart:math that contains a Random class to provide a random generator. It provides the nextInt method takes a maximum value and returns a random number.

int nextInt(int max)

minimum number is taken as zero as a minimum and the maximum number is the given max value.

Here is a simple example to generate Random Generator

import 'dart: math';

main() {
  var random = Random();
  print(random.nextInt(10)); //Random number between 0 to 10
  print(random.nextInt(100)); //Random number between 0 to 100
  print(random.nextInt(200)); //Random number between 0 to 100
  print(random.nextInt(1000)); //Random number between 0 to 1000
}
  • Imported the dart:math package into a program using the import keyword
  • Create a Random variable using the var keyword
  • Call nextInt() with seed number.
  • Random Number always generated between 0 to seed number

Output:

4
70
47
294

Dart random Number Generator within a range example

This is a generating random number between min and max value of ranges example.

import 'dart:math';

main() {
  var random = Random();
  int min = 10;
  int max = 20;
  var randomNumber = min + random.nextInt(max - min);
  print("$randomNumber is random number between $min and $max");
  print(randomNumber);
}

Output:

19 is a random number between 10 and 20
19

How to generate a random number in dart excluding a particular number?

For example, You want to generate a random number between 0 to 10 and the generated random number must not be 5.

Here is an example program to generate a Random Number excluding given number

import 'dart:math';

main() {
  var random = Random();
  var excludeNumber = 5;
  var randomNumber;
  do {
    randomNumber = random.nextInt(10);
  } while (randomNumber == excludeNumber);
  print(randomNumber);
}
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Perl How to: Find a Given Variable String is numeric or not

Dart/Flutter: Check if String is Empty, Null, or Blank example

How to generate Unique Id UUID in Dart or Flutter Programming| Dart or Flutterby Example

How to Sort List of numbers or String in ascending and descending in Dart or Flutter example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart Example - Program to check Leap year or not

Dart Example to count the number of digits in a number| Decimals Count in Dart