用于生成随机字节的通用库。
已弃用: 该模块在 SDK 49 中将不再可用。 请改用 expo-crypto。
expo-random
提供了用于创建强随机字节的原生接口。 使用 Random
,你可以创建与 Node.js 核心 crypto.randomBytes
API 等效的值。 expo-random
还可以与 expo-standard-web-crypto
配合使用,后者实现了用于生成随机字节的 W3C Crypto API。
Android Device | Android Emulator | iOS Device | iOS Simulator | Web |
---|---|---|---|---|
-
npx expo install expo-random
If you're installing this in a bare React Native app, you should also follow these additional installation instructions.
import * as Random from 'expo-random';
Random.getRandomBytes(byteCount)
Name | Type | Description |
---|---|---|
byteCount | number | A number within the range from |
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)
Name | Type | Description |
---|---|---|
byteCount | number | A number within the range from |
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
Promise<Uint8Array>
A promise that fulfills with an array of random bytes with the same length as the byteCount
.