Documentation for automated readers
A curated documentation index is available at: https://grafana.com/llms.txt
A complete documentation index is available at: https://grafana.com/llms-full.txt
These indexes can help with page discovery before fetching individual documents.
This page is also available in Markdown, which may be easier for automated readers and AI tools to parse than HTML. The Markdown version is available at https://grafana.com/docs/k6/v0.48.x/javascript-api/k6-crypto/randombytes.md, or by sending Accept: text/markdown to https://grafana.com/docs/k6/v0.48.x/javascript-api/k6-crypto/randombytes/. For broader documentation discovery, the curated index is available at https://grafana.com/llms.txt and the complete index is available at https://grafana.com/llms-full.txt.
Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.
randomBytes( int )
Note
A module with a better and standard API exists.
The new k6/experimental/webcrypto API partially implements the WebCryptoAPI, supporting more features than k6/crypto.
Return an ArrayBuffer object with a number of cryptographically random bytes. It will either return exactly the amount of bytes requested or will throw an exception if something went wrong.
| Parameter | Type | Description |
|---|---|---|
| int | integer | The length of the returned ArrayBuffer. |
Returns
| Type | Description |
|---|---|
| ArrayBuffer | An ArrayBuffer with cryptographically random bytes. |
Example
import crypto from 'k6/crypto';
export default function () {
const bytes = crypto.randomBytes(42);
const view = new Uint8Array(bytes);
console.log(view); // 156,71,245,191,56,...
}Was this page helpful?
Related resources from Grafana Labs

