一行代码写出随机数

 
 
var result = Math.random().toString(36).substring(2,6);

console.log(result);//包含0-9a-z
 
 

 

 
 

原理解析:javascript

  1. Math.random()生成一个随机数;
  2. toString(36)转成字符串,遵循编码为36进制;
  3. substring(2,6)字符串截取,从索引为2(包含索引2)截取到索引为6(不包含索引6)的4个字符。(详见substring()方法);
相关文章
相关标签/搜索