How to generate 6 digit random number

26.7k views Asked by At

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.

11

There are 11 answers

0
Günter Zöchbauer On BEST ANSWER
void main() {
  var rnd = new math.Random();
  var next = rnd.nextDouble() * 1000000;
  while (next < 100000) {
    next *= 10;
  }
  print(next.toInt());
}
0
manish bolbanda On

i had the same problem.although there are 3-4 ways to tackle it but i find the below one simple and sorted. simply check for the number of length match. find below code

Integer otp = new Random().nextInt(999999);
int noOfOtpDigit=6;
  while(Integer.toString(otp).length()!=noOfOtpDigit) {
  otp = new Random().nextInt(999999);
 }
String otpString = String.valueOf(otp);
0
Faizan Ahmad On

why not a generic function so you can use it anywhere? If the input is 1 then it will generate 1 digit number, if 2 then 2 digit number and so on.

import 'dart:math';

int generateRandomNumber(int input) {
  if (input < 1) {
    throw ArgumentError("Input should be at least 1");
  }

  int min = pow(10, input - 1).toInt();
  int max = pow(10, input).toInt() - 1;

  Random random = Random();
  int randomNumber = min + random.nextInt(max - min + 1);
  return randomNumber;
}

void main() {
  int userInput = 4; // Change this to the desired input value
  int randomOutput = generateRandomNumber(userInput);
  print("Generated Random Number: $randomOutput");
}
0
NirajPhutane On
import 'dart:math';

void main() {
  print(get6DigitNumber());
}

String get6DigitNumber(){
  Random random = Random();
  String number = '';
  for(int i = 0; i < 6; i++){
    number = number + random.nextInt(9).toString();
  }
  return number;
}
0
Kamran Bashir On

Here is your unlimited supply of six-digit random numbers

String getRandomNumber(){
    final r = Random();
    return List<int>.generate(6, (index) => r.nextInt(10)).fold<String>("", (prev, i) => prev += i.toString());
}
0
KompjoeFriek On

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.

1
Raouf Rahiche On

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);
}
0
Sean Vikoren On

The following class will generate an integer with 'n' digits, or a string with 'n' digits.

The numeric method will be much faster, but is limited in the number of digits.

import 'dart:math';

class RandomDigits {
  static const MaxNumericDigits = 17;
  static final _random = Random();

  static int getInteger(int digitCount) {
    if (digitCount > MaxNumericDigits || digitCount < 1) throw new RangeError.range(0, 1, MaxNumericDigits, "Digit Count");
    var digit = _random.nextInt(9) + 1;  // first digit must not be a zero
    int n = digit;

    for (var i = 0; i < digitCount - 1; i++) {
      digit = _random.nextInt(10);
      n *= 10;
      n += digit;
    }
    return n;
  }

  static String getString(int digitCount) {
    String s = "";
    for (var i = 0; i < digitCount; i++) {
      s += _random.nextInt(10).toString();
    }
    return s;
  }
}

void main() {
    print(RandomDigits.getInteger(6));
    print(RandomDigits.getString(36));
}

Output:

995723

198815207332880163668637448423456900

0
Farhan Syah On

If you want to get 6 digit value from 0 to 999999, you can add leading 0 if the number is less than 6 digits.

String r = Random().nextInt(999999).toString().padLeft(6, '0');
// example output: 025328
0
TechAurelian On

Here is a Dart extension method that will generate a non-negative random integer with a specified number of digits:

extension RandomOfDigits on Random {
  /// Generates a non-negative random integer with a specified number of digits.
  ///
  /// Supports [digitCount] values between 1 and 9 inclusive.
  int nextIntOfDigits(int digitCount) {
    assert(1 <= digitCount && digitCount <= 9);
    int min = digitCount == 1 ? 0 : pow(10, digitCount - 1);
    int max = pow(10, digitCount);
    return min + nextInt(max - min);
  }
}

In your case use it like this:

final random = Random();
print(random.nextIntOfDigits(6));
0
C_Sutt On

This is working for me in C#.

Random random = new Random();
string elementIndex = random.Next(100000, 999999).ToString();