---
name: "getrandom"
section: "2"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/getrandom"
---

# getrandom(2)

get random data

## NAME

**getrandom** – get random data

## LIBRARY

Standard C Library (libc, -lc)

## SYNOPSIS

**#include \<sys/random.h\>**

*ssize\_t*\

**getrandom**(*void \*buf*, *size\_t buflen*, *unsigned int flags*);

## DESCRIPTION

**getrandom**() fills *buf* with up to *buflen* bytes of random data.

The *flags* argument may include zero or more of the following:

- ‘GRND\_NONBLOCK’

  Return EAGAIN instead of blocking, if the [random(4)](/random/4.md) device has not yet been seeded. By default, **getrandom**() will block until the device is seeded.

- ‘GRND\_RANDOM’

  This flag does nothing on FreeBSD. */dev/random* and */dev/urandom* are identical.

- ‘GRND\_INSECURE’

  This flag is treated as an alternative name for GRND\_NONBLOCK. It is provided solely for API compatibility with Linux.

If the [random(4)](/random/4.md) device has been seeded, reads of up to 256 bytes will always return as many bytes as requested and will not be interrupted by signals.

## RETURN VALUES

Upon successful completion, the number of bytes which were actually read is returned. For requests larger than 256 bytes, this can be fewer bytes than were requested. Otherwise, -1 is returned and the global variable *errno* is set to indicate the error.

## ERRORS

The **getrandom**() operation returns the following errors:

- \[EAGAIN\]

  The ‘GRND\_NONBLOCK’ (or ‘GRND\_INSECURE’) flag was set and the [random(4)](/random/4.md) device was not yet seeded.

- \[EFAULT\]

  The *buf* parameter points to an invalid address.

- \[EINTR\]

  The sleep was interrupted by a signal.

- \[EINVAL\]

  An invalid *flags* was specified.

- \[EINVAL\]

  The requested *buflen* was larger than IOSIZE\_MAX.

## SEE ALSO

[arc4random(3)](/arc4random/3.md), [getentropy(3)](/getentropy/3.md), [random(4)](/random/4.md)

## STANDARDS

**getrandom**() is non-standard. It is present in Linux.

## HISTORY

The **getrandom**() system call first appeared in FreeBSD 12.0.

## CAVEATS

Unlike Linux, the GRND\_INSECURE flag on FreeBSD does not produce any output before the [random(4)](/random/4.md) device is seeded.
