Writing a 'for loop' for random no which is not zero(0) but still I'm getting zero(0)


Asked by abhijeet on November 23, 2021 (source).

I'm new to dart, and I'm writing the following for loop for random num which is not a zero, but it still returns zero to me:

  int Num = 1;
  for (var i = 1; i != 0 && i <= 6; i++) {
    final _random = Random();
    Num = _random.nextInt(i);
  print('Randon num is $Num');


Question answered by Jahn E (source).

A good place for problems like this is the dart api. This link provides you with everything you need.

Here it says the following:

nextInt(int max) → int Generates a non-negative random integer uniformly distributed in the range from 0, inclusive, to max, exclusive. [...] Also i represents the max value the nextInt() method will produce.

To generate a single random number:

import 'dart:math';

void main() {
  int maxNb = 10; 
  generateRandomNumber(int maxNb) {
    final int number = Random().nextInt(maxNb) + 1; 
    return number;
  int result = generateRandomNumber(maxNb);

To generate multiple random numbers:

import 'dart:math';

void main() {
  int maxNb = 10;
  int amount = 6;
  List randomNumbers = [];
  generateRandomNumbers(int amount, int maxNb) {
    for (var i = 1; i <= amount; i++) {
    final number = Random().nextInt(maxNb) + 1;

FYI: Variable names are NOT capitalized.