randomString(length, [charset])
此函数返回指定长度的随机字符串,可选择从自定义字符集中选取字符。
参数 | 类型 | 描述 |
---|---|---|
length | int | 随机字符串的长度 |
charset(可选) | string | 自定义字符列表 |
返回值
类型 | 描述 |
---|---|
any | 从字符数组中选取的随机项 |
示例
import { randomString } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';
export default function () {
const randomFirstName = randomString(8);
console.log(`Hello, my first name is ${randomFirstName}`);
const randomLastName = randomString(10, `aeioubcdfghijpqrstuv`);
console.log(`Hello, my last name is ${randomLastName}`);
const randomCharacterWeighted = randomString(1, `AAAABBBCCD`);
console.log(`Chose a random character ${randomCharacterWeighted}`);
}