Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate 6 digit random number

Tags:

flutter

dart

I want to generate a random six-digit number. I tried to use the Random class, but new Random().nextInt(999999) generates some numbers with less than six digits.

like image 426
Osama Gamal Avatar asked Jun 26 '18 13:06

Osama Gamal


People also ask

How do you generate a 6 digit random number in darts?

you can get a random number in this range (900000) and add 100000 to the random number you get: var rng = new Random(); var code = rng. nextInt(900000) + 100000; This will always give you a random number with 6 digits.


3 Answers

So you want just the numbers 100000 to (and including) 999999.

you can get a random number in this range (900000) and add 100000 to the random number you get:

var rng = new Random();
var code = rng.nextInt(900000) + 100000;

This will always give you a random number with 6 digits.

like image 106
KompjoeFriek Avatar answered Oct 12 '22 06:10

KompjoeFriek


void main() {
  var rnd = new math.Random();
  var next = rnd.nextDouble() * 1000000;
  while (next < 100000) {
    next *= 10;
  }
  print(next.toInt());
}
like image 32
Günter Zöchbauer Avatar answered Oct 12 '22 06:10

Günter Zöchbauer


you can also generate 6 different numbers and then concatenate them in one string and convert it to integer if you want

import 'dart:math';
main(){
  var rndnumber="";
  var rnd= new Random();
  for (var i = 0; i < 6; i++) {
  rndnumber = rndnumber + rnd.nextInt(9).toString();
  }
  print(rndnumber);
}
like image 4
Raouf Rahiche Avatar answered Oct 12 '22 06:10

Raouf Rahiche