菜单
文档breadcrumb arrow Grafana k6breadcrumb arrow JavaScript APIbreadcrumb arrow jslibbreadcrumb arrow utilsbreadcrumb arrow randomString(length, [charset])
开源

randomString(length, [charset])

此函数返回指定长度的随机字符串,可选择从自定义字符集中选取字符。

参数类型描述
lengthint随机字符串的长度
charset(可选)string自定义字符列表

返回值

类型描述
any从字符数组中选取的随机项

示例

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