js 获取随机数 Math.random()

// 结果为0-1间的一个随机数(包括0,不包括1)
var randomNum1 = Math.random();
//console.log(randomNum1); // 函数结果为入参的整数部分。
var randomNum2 = Math.floor(randomNum1);
//console.log(randomNum2); // 函数结果为入参四舍五入后的整数。
var randomNum3 = Math.round(randomNum1);
//console.log(randomNum3); // Math.ceil(n); 返回大于等于n的最小整数。
var randomNum4 = Math.ceil(Math.random() * 10); // 主要获取1到10的随机整数,取0的几率极小。
//console.log(randomNum4); // Math.round(n); 返回n四舍五入后整数的值。
var randomNum5 = Math.round(Math.random()); // 可均衡获取0到1的随机整数。
//console.log(randomNum5);
var randomNum6 = Math.round(Math.random() * 10); // 可基本均衡获取0到10的随机整数,其中获取最小值0和最大值10的几率少一半。
//console.log(randomNum6); // Math.floor(n); 返回小于等于n的最大整数。
var randomNum7 = Math.floor(Math.random() * 10); // 可均衡获取0到9的随机整数。
//console.log(randomNum7); // 获取最小值到最大值之前的整数随机数
function GetRandomNum(Min, Max) {
var Range = Max - Min;
var Rand = Math.random();
return(Min + Math.round(Rand * Range));
}
var num = GetRandomNum(1, 10);
//console.log(num); //获取n位随机数,随机来源chars
var chars = ['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'];
function generateMixed(n) {
var res = "";
for(var i = 0; i < n; i++) {
var id = Math.ceil(Math.random() * 35);
res += chars[id];
}
return res;
}
var num1 = generateMixed(6);
//console.log(num1);

http://www.cnblogs.com/banbu/archive/2012/07/25/2607880.html

js 获取随机数 Math.random()的更多相关文章

  1. js进阶解决浏览器缓存不能自动更新的问题(在ajax的url上带上一个参数,可以是日期,或者是随机数)(随机数Math.random)(取得日期的毫秒数:new Date().getTime();)

    js进阶解决浏览器缓存不能自动更新的问题(在ajax的url上带上一个参数,可以是日期,或者是随机数)(随机数Math.random)(取得日期的毫秒数:new Date().getTime();) ...

  2. js获取随机数

    js 获取随机数方法如下: 1.Math.random()表示 结果为0-1间的一个随机数(包括0,不包括1) : 返回指定范围的随机数(m-n之间)的公式 Math.random()*(n-m)+m ...

  3. js生成[n,m]的随机数,js如何生成随机数,javascript随机数Math.random()

    一.预备知识 Math.ceil();  //向上取整. Math.floor();  //向下取整. Math.round();  //四舍五入. Math.random();  //0.0 ~ 1 ...

  4. lua 随机数 math.random()和math.randomseed()用法

    用法一:  不给范围,就随机算一个0~1之间的小数: 用法二:给一个参数,就取1~n之间的随机数 用法三:给两个参数,就取m~n之间的随机数 math.randomseed()用法:     由于C中 ...

  5. 随机数Math.random()公式

    1. 0-x之间的随机数: Math.round(Math.random()*x); 2. x至y之间的随机数 Math.round(Math.random()*(y-x)+x); 3. 1-x之间的 ...

  6. JS实现使用Math.random()函数生成n到m间的随机数字

    Math.random()函数返回0和1之间的伪随机数,可能为0,但总是小于1,[0,1) 生成n-m,包含n但不包含m的整数: 第一步算出 m-n的值,假设等于w 第二步Math.random()w ...

  7. java随机数Math.random()

    double random=Math.random();//返回[0,1)随机数 (int)(Math.random()*6)//返回0-5:随机数 (int)(Math.random()*6+1)/ ...

  8. js 获取随机数

    返回 m 到 n 的随机整数 <script type="text/javascript"> function randomNumber(m.n){ return Ma ...

  9. js random获取随机数,获取任意范围内随机整数

     壹 ❀ 引 想着好久没做笔试题了,去GitHub找了面试相关的项目,结果被第一道题难住了.....说难其实也不难,而是我忘记了取范围随机整数怎么写了,不可否认如果当时是我在笔试,肯定也凉了,那么就由 ...

随机推荐

  1. Oracle字符函数length substr concat实例

    --字符函数 --伪表dual --(1)求字符串长度 select length('123.456/-*') from dual --(2)截取函数求字符串的子串 ,) from dual --(3 ...

  2. SSH框架整合实现Java三层架构实例(一)

    HTML前台发送请求代码: <tr> <td>选择收派时间</td> <td> <input type="text" name ...

  3. v-router几种定义方式

    第一种 const router = new VueRouter({ routes: [{ path: '/newSongs', component: require('../views/NewSon ...

  4. Dart语法基础

    hello world // Define a function. printNumber(num aNumber) { print('The number is $aNumber.'); // Pr ...

  5. MySQL简介及安装

    一.DBA工作内容及课程体系 二.MySQL课程体系介绍 三.DBA的职业素养 四.MySQL简介及安装 01 什么是数据? 02 什么是数据库管理系统 03 数据库管理系统种类 04 MySQL发展 ...

  6. Pyspark spark-submit 集群提交任务以及引入虚拟环境依赖包攻略

    网上提交 scala spark 任务的攻略非常多,官方文档其实也非常详细仔细的介绍了 spark-submit 的用法.但是对于 python 的提交提及得非常少,能查阅到的资料非常少导致是有非常多 ...

  7. 版本控制Git使用最佳实践

    总结版本控制Git的使用,应明确有哪些具体的场景 应用场景  紧急上线(hotfix)  功能开发(feature)  测试(dev/release)  生产(master) 紧急上线 4.git c ...

  8. vs code軟件操作

    https://www.imooc.com/article/39349 https://www.html.cn/archives/8144

  9. ERROR org.hibernate.internal.SessionImpl - HHH000346: Error during managed flush [object references an unsaved transient instance - save the transient instance before flushing: cn.itcast.domain.Custom

    本片博文整理关于Hibernate中级联策略cascade和它导致的异常: Exception in thread "main" org.hibernate.TransientOb ...

  10. python----函数的动态传参

    函数的动态传参 *args 将所有的实参的位置参数聚合到一个元组,并将这个元组赋值给args 有些时候,对于函数,传入的实参数量可能是不固定的,也就是动态的,这个时候我们就需要用到函数的动态传参.下面 ...