JavaScript中的SecureRandom?

Tro*_*nic 5 javascript ruby random hash

SecureRandom.hex()JavaScript中是否有类似(ruby)函数为我生成随机哈希?

Koo*_*Inc 3

JS 中没有这样的辅助函数。您可以使用以下方法生成相当随机的哈希值:

function hex(n){
 n = n || 16;
 var result = '';
 while (n--){
  result += Math.floor(Math.random()*16).toString(16).toUpperCase();
 }
 return result;
}
Run Code Online (Sandbox Code Playgroud)

您可以修改它以形成一个 guid:

function generateGuid(){
 var result = '', n=0;
 while (n<32){
  result += (~[8,12,16,20].indexOf(n++) ? '-': '') +    
            Math.floor(Math.random()*16).toString(16).toUpperCase();
 }
 return result;
}
Run Code Online (Sandbox Code Playgroud)