or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

address.mdbase-encoding.mdcrypto-init.mdethereum.mdhashing.mdindex.mdjson-encryption.mdkey-derivation-pbkdf.mdkey-derivation.mdkeypairs.mdmnemonic.mdrandom.mdsignatures.md

random.mddocs/

0

# Random Number Generation

1

2

Cryptographically secure random number generation in various formats for keys, nonces, and other security-critical randomness.

3

4

## Capabilities

5

6

### Random Bytes

7

8

Generate cryptographically secure random bytes.

9

10

```typescript { .api }

11

/**

12

* Generate random bytes

13

* @param bitLength - Length in bits (default: 256)

14

* @returns Random bytes as Uint8Array

15

*/

16

function randomAsU8a(bitLength?: number): Uint8Array;

17

18

/**

19

* Generate random hex string

20

* @param bitLength - Length in bits (default: 256)

21

* @returns Random hex string with 0x prefix

22

*/

23

function randomAsHex(bitLength?: number): string;

24

25

/**

26

* Generate random number

27

* @param bitLength - Length in bits (default: 53)

28

* @returns Random number within JavaScript safe integer range

29

*/

30

function randomAsNumber(bitLength?: number): number;

31

```

32

33

**Usage Examples:**

34

35

```typescript

36

import { randomAsU8a, randomAsHex, randomAsNumber } from "@polkadot/util-crypto";

37

38

// Generate 32 random bytes (256 bits)

39

const randomBytes = randomAsU8a(256);

40

console.log(randomBytes.length); // 32

41

42

// Generate random hex string

43

const randomHex = randomAsHex(128); // 16 bytes as hex

44

console.log(randomHex); // "0x..."

45

46

// Generate random number (up to 53 bits for JavaScript safety)

47

const randomNum = randomAsNumber(32);

48

console.log(randomNum); // Random 32-bit integer

49

```