For AI agents: the complete documentation index is available at /apps-plugin/docs/llms.txt, the full documentation bundle is available at /apps-plugin/docs/llms-full.txt, and this page is available as Markdown at /apps-plugin/docs/js-api/device/crypto/getRandomValues.md.

getRandomValues

获取安全随机数。

扫码预览

扫码预览 getRandomValues

支持版本

前端库版本不低于 0.0.25。

支持平台

平台支持情况
Android支持
iOS支持
PC支持
HarmonyOS支持

支持场景

场景支持情况
页面支持
卡片不支持

使用说明

  • 生成的字节数越大耗时越长,建议按实际需要申请长度

调用方式

异步 API

getRandomValues(params: GetRandomValuesParams): Promise<GetRandomValuesResult>

入参

名称类型必填默认值约束说明
lengthnumber是-取值范围 0 到 1048576 的整数,超出范围会抛出 RangeError需要生成的字节数。

调用示例

import { getRandomValues } from '@doubao-dev/framework/api';

const { randomValues } = await getRandomValues({ length: 16 });
const bytes = new Uint8Array(randomValues);

console.log(bytes);

成功返回

名称类型必返说明
randomValuesArrayBuffer是生成的随机字节。

返回示例

{
  "randomValues": {}
}