随机生成uuid

3 阅读1分钟
	function uuid() {
		const s = [];
		const hexDigits = "0123456789abcdefghijklmnopqrstuvwxyz";
		for (let i = 0; i < 36; i++) {
			// Math.floor(Math.random() * 0x10) 生成 0 到 16 之间的随机整数 - c72f0352-10ec-439c-beb6-0eb851b7dfd0
			// Math.floor(Math.random() * 0x23) 生成 0 到 34 之间的随机整数 - m2p6s5k1-x8nd-49co-91rw-bnaji1hjbb8l
			s[i] = hexDigits.substr(Math.floor(Math.random() * 0x23), 1);
		}
		s[14] = "4";
		s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
		s[8] = s[13] = s[18] = s[23] = "-";
		const uuid = s.join("");
		return uuid;
	}