🏂

「Flutter」ランダムな文字列

2022/10/10に公開

日本語版

今回は、ランダムな文字列を生成する方法を紹介します。

まず、変数を宣言して、生成したい文字列を代入します。

random_string.dart
const chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZ1234567890';

では、Random クラスの変数を宣言してみましょう。

random_string.dart
Random rnd = Random();

そこで、単語の長さを表す数値を受け取って、その単語を String として返す関数が必要になります。

もちろん、forループを使うこともできますが、ここでは String.fromCharCodes() メソッドを使いましょう。

String.fromCharCodes()

指定された charCodes を含む新しい文字列を確保する。

final string = String.fromCharCodes([68]);
print(string); // D

charCodesを使うので、そのcharを生成するものが必要です。
そのために最適なのが Iterable.generate() メソッドです。

Iterable.generate()

動的に要素を生成する Iterable を作成します。

生成されたイテラブルは count 個の要素を持ち、インデックス n の要素は generator(n) を呼ぶことで計算される。値はキャッシュされないので,各反復処理で再び計算されます.

必要な長さの単語を生成する必要がある。そして、char 文字列からランダムなカーの連鎖を生成します。

Iterable.generate(length,(_) =>)
	chars.codeUnitAt(rnd.nextInt(chars.length)))

これらのことを1つの関数にまとめましょう。

random_string.dart
const chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
Random rnd = Random();

String getRandomString(int length) =>
	String.fromCharCodes(Iterable.generate(length,(_) =>
	chars.codeUnitAt(rnd.nextInt(chars.length))));

ランダムな String を生成するには、この関数に数値を指定します。

void main() {
	const chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
	Random rnd = Random();
	String getRandomString(int length) =>
               String.fromCharCodes(Iterable.generate(
	       length,(_) => chars.codeUnitAt(
	       rnd.nextInt(chars.length))));
	      
	print(getRandomString(5)); //f6jKa
}

English ver.

In this article, I will introduce the way how to generate random String.

First let's declare a variable and assign a string of characters that we want to generate.

random_string.dart
const chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';

Now lets declare a variable from Random class.

random_string.dart
Random rnd = Random();

So, we going to need a function that will take a number of how long word should be and return that word as a String.

Of course, we can use for loop, but let's use String.fromCharCodes() method.

String.fromCharCodes()

Allocates a new string containing the specified charCodes.

final string = String.fromCharCodes([68]);
print(string); // D

Because we going to use charCodes, we need something that will generate those codes.
And for that, the best way is Iterable.generate()method.

Iterable.generate()

Creates an Iterable which generates its elements dynamically.

The generated iterable has count elements, and the element at index n is computed by calling generator(n). Values are not cached, so each iteration computes the values again.

We need to generate word that is long as we need to be. And generate random chain of cahrs from char String.

Iterable.generate(length,(_) =>
	chars.codeUnitAt(rnd.nextInt(chars.length)))

We just to have to put all those things in one function.
So our code should looks like this.

random_string.dart
const chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
Random rnd = Random();

String getRandomString(int length) =>
	String.fromCharCodes(Iterable.generate(length,(_) =>
	chars.codeUnitAt(rnd.nextInt(chars.length))));

To generate a random String, just put a number in our function.

Example

void main() {
	const chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
	Random rnd = Random();
	String getRandomString(int length) =>
               String.fromCharCodes(Iterable.generate(
	       length,(_) => chars.codeUnitAt(
	       rnd.nextInt(chars.length))));
	      
	print(getRandomString(5)); //f6jKa
}

Discussion