Get random bytes

# Random\Randomizer::getBytes

(PHP 8 >= 8.2.0)

Random\Randomizer::getBytes — Get random bytes

### 说明

public function 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.

### 参数

`length`

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

### 返回值

A string containing the requested number of random bytes.

### 错误／异常

- If the value of `length` is less than `1`, a ValueError will be thrown.
- 底层 `Random\Randomizer::$engine` 的 Random\Engine::generate() 方法抛出的任何 Throwable。

### 示例

示例 #1 Random\Randomizer::getBytes() example

    <?php
    $r = new \Random\Randomizer();

    echo bin2hex($r->getBytes(8)), "\n";
    ?>

以上示例的输出类似于：

    ebdbe93cd56682c2

### 参见

- random_bytes() - Get cryptographically secure random bytes
- bin2hex() - 将二进制数据转换为十六进制表示
- base64_encode() - 使用 MIME base64 对数据进行编码
- Random\Randomizer::getBytesFromString() - Get random bytes from a source string

`↑` and `↓` to navigate • `Enter` to select • `Esc` to close • `/` to open