Today I Learned

Random number generator with Web Crypto API

February 19, 2022

The Crypto interface allows access to a cryptographically strong random number generator and to cryptographic primitives.

The Web Crypto API is accessed through the global crypto property, which is a Crypto object.

Crypto.randomUUID()

Return a containing a randomly generated, 36 character long v4 UUID.

/* Assuming that self.crypto.randomUUID() is available */

let uuid = self.crypto.randomUUID();
console.log(uuid); // for example "36b8f84d-df4e-4d49-b662-bcde71a8764f"

Reference

https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID


© 2025 - Written by Vuong Vu. Connect with me on LinkedIn.