DEV Community

Discussion on: What the Hex!? (how to generate random hex color codes in javascript)

lukaszahradnik profile image
Lukáš Zahradník • Edited on

Fair point, easy fix

const generateHex = () => `#${Math.floor(Math.random() * 256**3).toString(16).padStart(6, '0')}`

or more readable

const generateHex = () => {
  const hex = Math.floor(Math.random() * 256**3).toString(16).padStart(6, '0')
  return `#${hex}`

//edit: Btw, because of your use of Math.round values don't have the same probability distribution