# Random\Randomizer::getBytes

(PHP 8 >= 8.2.0)

Random\Randomizer::getBytes — Get random bytes

### Descripción

public Random\Randomizer::getBytes(int `$length`): string

Generates a string containing uniformly selected random bytes with the requested `length`.

As the returned bytes are selected completely randomly, the resulting string is likely to contain unprintable characters or invalid UTF-8 sequences. It may be necessary to encode it before transmission or display.

### Parámetros

`length`

  The length of the random string that should be returned in bytes; must be `1` or greater.

### Valores devueltos

A string containing the requested number of random bytes.

### Errores/Excepciones

- If the value of `length` is less than `1`, a ValueError will be thrown.
- Any Throwables thrown by the Random\Engine::generate() method of the underlying `Random\Randomizer::$engine`.

### Ejemplos

Ejemplo #1 Random\Randomizer::getBytes() example

`<?php
$r = new \Random\Randomizer();
echo bin2hex($r->getBytes(8)), "\n";
?>`

El resultado del ejemplo sería algo similar a:

    ebdbe93cd56682c2

### Ver también

- random_bytes() - Get cryptographically secure random bytes
- bin2hex() - Convierte datos binarios en su representación hexadecimal
- base64_encode() - Codifica datos con MIME base64
- Random\Randomizer::getBytesFromString() - Get random bytes from a source string