javascript自动生成包含数字与字符的随机字符串
网络编程 2021-07-04 21:47www.168986.cn编程入门
这篇文章主要介绍了javascript自动生成包含数字与字符的随机字符串,涉及Math.random()和Math.floor()两个函数的使用技巧,需要的朋友可以参考下
本文实例讲述了javascript自动生成包含数字与字符的随机字符串的方法。分享给大家供大家参考。具体如下
这里主要用到Math.random() 和 Math.floor() 两个函数
Math.random() -- 返回0和1之间的伪随机数 可能为0,但总是小于1,[0,1)
Math.floor() -- 向下取整,舍掉小数字后的值
实现随机多位数的方法
代码如下:
/
随机生成数字
@param num 生成数字位数
/
function randomNumber(num){
return ''+Math.floor(Math.random() num) ;
}
随机生成数字
@param num 生成数字位数
/
function randomNumber(num){
return ''+Math.floor(Math.random() num) ;
}
javascript 随机生成字符
以下实例为随机 A-Z / a-z区间 中随机出现的字符
代码如下:
/
随机生成
@param data json数据 例{"start":0,"end":2,"number":5,"upper":true}
@param start 开始下标
@param end 结束下标
@param number 生成位数
@param upper 是否大写 默认为小写
/
function randomLetter(data){
var letterData = "";
var lowercase = new Array("a","b","c","d","e","f","g","h","i","g","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
var uppercase = new Array("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");
var start = data.start;
var end = data.end;
var number = data.number;
var upper = data.upper;
var length = end - start;
for(var i= 0;i < number; i++){
var index = Math.floor(Math.random() length) + start;
var letter = "";
if(upper){
letter = uppercase[index];
}else{
letter = lowercase[index];
}
letterData += letter;
}
return letterData;
}
随机生成
@param data json数据 例{"start":0,"end":2,"number":5,"upper":true}
@param start 开始下标
@param end 结束下标
@param number 生成位数
@param upper 是否大写 默认为小写
/
function randomLetter(data){
var letterData = "";
var lowercase = new Array("a","b","c","d","e","f","g","h","i","g","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
var uppercase = new Array("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");
var start = data.start;
var end = data.end;
var number = data.number;
var upper = data.upper;
var length = end - start;
for(var i= 0;i < number; i++){
var index = Math.floor(Math.random() length) + start;
var letter = "";
if(upper){
letter = uppercase[index];
}else{
letter = lowercase[index];
}
letterData += letter;
}
return letterData;
}
引用
代码如下:
alert(new Date().Format("yyyy-MM-dd hh:mm:ss"));
希望本文所述对大家的javascript程序设计有所帮助。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程