1、随机获取数
//获取min-max之间的整数
function showNum(min,max){
var numInt=Math.floor(Math.random()*(max-min)+min);
return numInt;
}
2、随机获取颜色
//获取随机颜色
function showColor(){
var r=showNum(0,256);
var g=showNum(0,256);
var b=showNum(0,256);
//模板字符串
var result=`rgb(${r},${g},${b})`;
//在屏幕显示颜色
document.body.style.backgroundColor=result;
return result;
}
3、随机获取验证码(包含数字+字母(大写))
//随机获取验证码(a=验证码的位数)
function showCode(a){
var code="";
var codeLength=a;
var randomCode=[0,1,2,3,4,5,6,7,8,9,"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
for(var i=0;i<codeLength;i++){
var index=showNum(0,36);
code=code+randomCode[index];
}
return code;
}
三个随机小练习,直接调用函数使用;