generate static method

String generate([
  1. int length = 32,
  2. Random? random
])

Generates a random alphanumeric string length characters long.

If random is provided, it will be used to generate the string, otherwise the default implementation of Random will be used.

Implementation

static String generate([int length = 32, Random? random]) {
  assert(length > 0);

  random ??= Random();

  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);
}