Guides
Plan-enterprise-icon
Expo Application Services
API Reference

Random

expo-random provides a native interface for creating strong random bytes. With Random you can create values equivalent to Node.js core crypto.randomBytes API. expo-random also works with expo-standard-web-crypto, which implements the W3C Crypto API for generating random bytes.

Platform Compatibility

Android DeviceAndroid EmulatoriOS DeviceiOS SimulatorWeb
Status-success-iconStatus-success-iconStatus-success-iconStatus-success-iconStatus-success-icon

Installation

Terminal
→ 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

  • Undo-iconUint8Array

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.

  • Undo-iconPromise<Uint8Array>

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

  • Message-iconAsk a question on the forums about Random
  • Github-iconView open bug reports for Random
  • Code-iconView source code for Random
  • Build-iconView package in npm Registry
  • Edit-iconEdit this page

Was this doc helpful?