This tutorial shows you how to generate a random String in Dart.
At the time this post was written, Dart doesn't have any function that can be used to directly generate a random String. However, you can utilize the Random
class which supports random number generation for generating a random string. This tutorial gives you examples of how to do it. You can apply the examples below in any Dart framework including Flutter.
Generate Random String from Character Set
The idea is defining a character set containing the list of allowed characters. With this approach, you can define your own custom character set. For each character in the randomly generated string, pick one character from the character set randomly.
To pick a random character from the character set, we can use Random
's nextInt
method, which is used to generate a random integer. Pass the length of the character set when calling the nextInt
method, so that it will only generate a random integer from 0 (inclusive) up to the length of the character set (exclusive). To use the Random
class, you need to import dart:math
.
In the example below, we use Iterable.generate
to generate the list of characters. For each generated character, we pick one character from the character set at a random index. Then, convert the list into a String by using the join
method.
final Random random = Random();
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
String generateRandomString(chars, int length) => Iterable
.generate(length, (idx) => chars[random.nextInt(chars.length)])
.join();
print(generateRandomString(chars, 10));
Generate Random String from Character Codes
Each character in Dart can be represented as an integer code unit. It's possible to convert a character into its code unit and vice versa. If you want to generate a random string where the character set is within specific code unit ranges, you can use this approach instead of defining the characters one by one.
First, we need to provide the list of code units stored in a List
. Then, use Iterable.generate
to generate the list of selected code units by randomly picking one for each generated character. To convert the code units into a string, use String.fromCharCodes
factory method.
String generateRandomStringFromCharCodes(List<int> charCodes, int length) => String.fromCharCodes(
Iterable.generate(length, (idx) => charCodes[random.nextInt(charCodes.length)])
);
print(generateRandomStringFromCharCodes(List.generate(10, (i) => i + 100), 10));
Generate Random Numeric, Alphabetic, and Alphanumeric String
For alphabetic and numeric characters, the code units are the same as the ASCII codes. The range for numeric characters from 0-9 is 48 ~ 57, while the range for uppercase and lowercase alphabetic characters are 65 ~ 90 and 97 ~ 122 respectively. We can use the generateRandomStringFromCharCodes
method above by passing the allowed code units.
First, create a List
of integers that contains the list of code units. For example, if you want to generate a random numeric string, you need to create a List
whose elements consist of integers from 48 to 57. Then, pass the List
to the generateRandomStringFromCharCodes
method above. Below are the examples for generating random numeric, alphabetic, uppercase alphabetic, lowercase alphabetic, and alphanumeric characters.
const numericLowerLimit = 48; // 0
const numericUpperLimit = 57; // 9
const upperCaseAlphabeticLowerLimit = 65; // 'A'
const upperCaseAlphabeticUpperLimit = 90; // 'Z'
const lowerCaseAlphabeticLowerLimit = 97; // 'a'
const lowerCaseAlphabeticUpperLimit = 122; // 'z'
final List<int> numericCharCodes = List
.generate(numericUpperLimit - numericLowerLimit + 1, (i) => i + numericLowerLimit)
.toList();
final List<int> alphabeticCharCodes = List
.generate(lowerCaseAlphabeticUpperLimit - upperCaseAlphabeticLowerLimit + 1, (i) => i + upperCaseAlphabeticLowerLimit)
.where((i) => i <= upperCaseAlphabeticUpperLimit || i >= lowerCaseAlphabeticLowerLimit)
.toList();
final List<int> lowerCaseAlphabeticCharCodes = List
.generate(lowerCaseAlphabeticUpperLimit - lowerCaseAlphabeticLowerLimit + 1, (i) => i + lowerCaseAlphabeticLowerLimit)
.toList();
final List<int> upperCaseAlphabeticCharCodes = List
.generate(upperCaseAlphabeticUpperLimit - upperCaseAlphabeticLowerLimit + 1, (i) => i + upperCaseAlphabeticLowerLimit)
.toList();
final List<int> alphaNumericCharCodes = List
.generate(lowerCaseAlphabeticUpperLimit - numericLowerLimit + 1, (i) => i + numericLowerLimit)
.where((i) => i <= numericUpperLimit
|| (i >= upperCaseAlphabeticLowerLimit && i <= upperCaseAlphabeticUpperLimit)
|| i >= lowerCaseAlphabeticLowerLimit
)
.toList();
String generateRandomNumericString(int length) =>
generateRandomStringFromCharCodes(numericCharCodes, length);
String generateRandomAlphabeticString(int length) =>
generateRandomStringFromCharCodes(alphabeticCharCodes, length);
String generateRandomUpperCaseAlphabeticString(int length) =>
generateRandomStringFromCharCodes(upperCaseAlphabeticCharCodes, length);
String generateRandomLowerCaseAlphabeticString(int length) =>
generateRandomStringFromCharCodes(lowerCaseAlphabeticCharCodes, length);
String generateRandomAlphaNumericString(int length) =>
generateRandomStringFromCharCodes(alphaNumericCharCodes, length);
print(generateRandomNumericString(10));
print(generateRandomAlphabeticString(10));
print(generateRandomUpperCaseAlphabeticString(10));
print(generateRandomLowerCaseAlphabeticString(10));
print(generateRandomAlphaNumericString(10));
Summary
For generating a random string in Dart without any library, you can define a character set and randomly choose one of the characters for each generated character. Alternatively, you can define the code units of the allowed characters, randomly select one for each generated character, and convert the result using String.fromCharCodes
method. The nextInt
method of Dart's Random
class can be used to randomly select the index of a character from the character set or code unit list.
You can also read about: