如何用js随机产生四个字母?

JavaScript010

如何用js随机产生四个字母?,第1张

所有随机的东西都要用到随机数,js中是用到Math.random()这个方法,获取0~1之间的数。

1、Math.random()是令系统随机选取大于等于 0.0 且小于 1.0 的伪随机 double 值,是Java语言常用代码。例如:double a=Math.random()*(3-1)+1,设置一个随机1到3的变量。

2、生成一个4位的随机字符串代码如下:

public static void main(String[]args){

String result=""

for(inti=0i<4i++){

//生成97-122的int型的整型

int intValue=(int)(Math.random()*26+97)

//将intValue强制转化成char类型后接到result后面

result=result+(char)intValue

}

//输出字符串

System.out.println(result)

}

function 随机(min,max) {

return Math.floor(Math.random()*(max-min))+min

}

随机(1000,9999)