generate static method

String generate({
  1. int length = 32,
  2. int? seed,
  3. bool secure = false,
})

Generates a random alphanumeric string length characters long.

Implementation

static String generate({int length = 32, int? seed, bool secure = false}) {
  assert(length > 0);
  assert(!secure || seed == null, 'Secure [Random]s don\'t accept seeds.');

  final random = secure ? Random.secure() : Random(seed);

  final charCodes = List<int>.generate(length, (_) {
    late int codeUnit;
    switch (random.nextInt(3)) {
      case 0:
        codeUnit = random.nextInt(10) + 48;
        break;
      case 1:
        codeUnit = random.nextInt(26) + 65;
        break;
      case 2:
        codeUnit = random.nextInt(26) + 97;
        break;
    }
    return codeUnit;
  });

  return String.fromCharCodes(charCodes);
}