首页指南参考教程

Expo Random

GitHub

npm

用于生成随机字节的通用库。


已弃用: 该模块在 SDK 49 中将不再可用。 请改用 expo-crypto

expo-random 提供了用于创建强随机字节的原生接口。 使用 Random,你可以创建与 Node.js 核心 crypto.randomBytes API 等效的值。 expo-random 还可以与 expo-standard-web-crypto 配合使用,后者实现了用于生成随机字节的 W3C Crypto API。

Platform Compatibility

Android DeviceAndroid EmulatoriOS DeviceiOS SimulatorWeb

安装

Terminal
npx expo install expo-random

If you're installing this in a bare React Native app, you should also follow these additional installation instructions.

API

import * as Random from 'expo-random';

Methods

Random.getRandomBytes(byteCount)

NameTypeDescription
byteCountnumber

A number within the range from 0 to 1024. Anything else will throw a TypeError.


Generates completely random bytes using native implementations. The byteCount property is a number indicating the number of bytes to generate in the form of a Uint8Array. Falls back to Math.random during development to prevent issues with React Native Debugger.

Returns

  • Uint8Array

An array of random bytes with the same length as the byteCount.

Random.getRandomBytesAsync(byteCount)

NameTypeDescription
byteCountnumber

A number within the range from 0 to 1024. Anything else will throw a TypeError.


Generates completely random bytes using native implementations. The byteCount property is a number indicating the number of bytes to generate in the form of a Uint8Array.

Returns


A promise that fulfills with an array of random bytes with the same length as the byteCount.

Expo 中文网 - 粤ICP备13048890号