方法一
踏上JAVASCRIPT的科技奇幻之旅:在数字边界揭示奇迹,运用Math.random()神秘的力量创造创新和令人惊叹的技术
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getUuid() { | |
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | |
var r = (Math.random() * 16) | 0, | |
v = c == 'x' ? r : (r & 0x3) | 0x8; | |
return v.toString(16); | |
}); | |
} |
方法二
crypto 提供了一个名为 randomUUID 的方法,该方法用于生成一个随机的 UUID(通用唯一标识符)。UUID是一个由数字和字母组成的标识符,具有全球唯一性。通过调用 randomUUID 方法,你可以轻松地获取一个不可预测且独一无二的标识符,适用于各种需要唯一性的应用场景。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const UUID = crypto.randomUUID(); | |
console.log(UUID) |
方法三
使用crypto提供的getRandomValues方法可以生成随机值。这个方法通常用于在Web应用程序中产生高质量的随机数,以用于加密、安全哈希和其他安全目的。通过调用这个方法,开发者能够获取由操作系统提供的真正随机的字节序列,从而增强应用程序的安全性。在密码学和安全领域,随机性对于确保数据的机密性和完整性至关重要,getRandomValues方法为开发者提供了一个可靠的方式来获取这种强随机性。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getUuid() { | |
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)); | |
} | |
console.log(getUuid()) |
实际开发中推荐的方法
在线生成uuid:https://www.uuid.online
回复删除