generateCode function

String generateCode(
  1. int length,
  2. {int seed = 0,
  3. String charSet = "23456789abcdefghjkmnpqrstuvwxy"}
)

Generates and returns a random string with the number of characters given by length.

If seed is given, the random generation can be seeded.

Only the characters given in charSet will be included in the randomly generated string.

Characters such as 1, l, and i, which are indistinguishable in some fonts, are eliminated by default.

lengthで与えられた文字数でランダムな文字列を生成して返します。

seedを与えた場合、ランダム生成にシードを与えることができます。

charSetで与えた文字のみがランダム生成する文字列に含まれます。

1liなどフォントによっては見分けがつかない文字をデフォルトでは排除しています。

Implementation

String generateCode(
  int length, {
  int seed = 0,
  String charSet = "23456789abcdefghjkmnpqrstuvwxy",
}) {
  final _length = charSet.length;
  final rand = seed != 0 ? Random(seed) : Random();
  final codeUnits = List.generate(
    length,
    (index) {
      final n = rand.nextInt(_length);
      return charSet.codeUnitAt(n);
    },
  );
  return String.fromCharCodes(codeUnits);
}