---
name: "libressl-BN_rand"
section: "3"
platform: "linux"
package: "libressl"
version: "4.4.0-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/libressl-BN_rand"
---

# libressl-BN\_rand(3)

generate pseudo-random number

## NAME

**BN\_rand**, **BN\_rand\_range**, **BN\_pseudo\_rand**, **BN\_pseudo\_rand\_range** – generate pseudo-random number

## SYNOPSIS

Crypto Library (libcrypto, -lcrypto)

**#include \<openssl/bn.h\>**

*int*\

**BN\_rand**(*BIGNUM \*rnd*, *int bits*, *int top*, *int bottom*);

*int*\

**BN\_rand\_range**(*BIGNUM \*rnd*, *const BIGNUM \*range*);

## DESCRIPTION

**BN\_rand**() generates a cryptographically strong pseudo-random number of *bits* in length and stores it in *rnd*. If *top* is BN\_RAND\_TOP\_ANY, the most significant bit of the random number can be zero. If *top* is BN\_RAND\_TOP\_ONE, the most significant bit is set to 1, and if *top* is BN\_RAND\_TOP\_TWO, the two most significant bits of the number will be set to 1, so that the product of two such random numbers will always have 2\**bits* length. If *bottom* is BN\_RAND\_BOTTOM\_ODD, the number will be odd; if it is BN\_RAND\_BOTTOM\_ANY, it can be odd or even. The value of *bits* must be zero or greater. If *bits* is +1 then *top* cannot be BN\_RAND\_TOP\_TWO.

**BN\_rand\_range**() generates a cryptographically strong pseudo-random number *rnd* in the range 0 \<= *rnd* \< *range*.

**BN\_pseudo\_rand**() is a deprecated alias for **BN\_rand**(), and **BN\_pseudo\_rand\_range**() for **BN\_rand\_range**().

## RETURN VALUES

The functions return 1 on success, 0 on error. The error codes can be obtained by ERR\_get\_error(3).

## SEE ALSO

BN\_new(3)

## HISTORY

**BN\_rand**() first appeared in SSLeay 0.5.1 and has been available since OpenBSD 2.4.

The *top* == -1 case and the function **BN\_rand\_range**() first appeared in OpenSSL 0.9.6a and have been available since OpenBSD 3.0.
