{

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


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

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 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 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 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 exclude 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

Dart| Flutter How to: Find a Given String is numeric or not

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

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

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example