一:生成一个16位的随机数
function generateRandomNumber() { var randomNumber = Math.floor(Math.random() * 10000000000000000); var randomString = randomNumber.toString().padStart(16, '0'); return randomString; } var randomNum = generateRandomNumber(); console.log(randomNum);
二:生成一个由数字和字母组成的16位随机字符串
function generateRandomString() { var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; var randomString = ''; for (var i = 0; i <16; i++) { var randomIndex = Math.floor(Math.random() * characters.length); randomString += characters.charAt(randomIndex); } return randomString; } var randomStr = generateRandomString(); console.log(randomStr);
标签:16,randomString,randomNum,生成,随机数,var From: https://www.cnblogs.com/it-girl-maxiaotiao/p/17652077.html