Math.random():获取0~1随机数

Math.floor() method rounds a number DOWNWARDS to the nearest integer, and returns the result. (小于等于 x,且与 x 最接近的整数。)
其实返回值就是该数的整数位:
Math.floor(0.666)   -->  0
Math.floor(39.2783)   -->  39

所以我们可以使用Math.floor(Math.random())去获取你想要的一个范围内的整数。
如:现在要从1~52内取一个随机数:
首先Math.random()*52  //这样我们就能得到一个 >=0 且 <52的数
然后加1:Math.random()*52 + 1    //现在这个数就 >=1 且 <53
再使用Math.floor取整

最终: Math.floor(Math.random()*52 + 1)

这就能得到一个取值范围为1~52的随机整数了.

-----------------------------------------------------------------------------------------------------------------------------------------

Math.(random/round/cell/floor)随机数的用法

Math.random()       返回值是一个大于等于0,且小于1的随机数

Math.random()*N    返回值是一个大于等于0,且小于N的随机数

Math.round()         四舍五入的取整
Math.ceil()            向上取整,如Math.cell(0.3)=1 、又如Math.ceil(Math.random()*10) 返回1~10
Math.floor()           向下取整,如Math.floor(0.3)=0、又如Math.floor(Math.random()*10)返回0~9

引申:
Math.round(Math.random()*15)+5;   返回5~20随机数

Math.round(Math.random()*(y-x))+x;   返回x~y的随机数,包含负数。

Math.floor(Math.random()*3+1)的更多相关文章

  1. Math.floor(Math.random() * array.length),splice

    1.Math.floor(Math.random() * array.length) 返回长度内的索引 eg: changeLimit () { function getArrayItems(arr, ...

  2. Number(),parseInt(),parseFloat(),Math.round(),Math.floor(),Math.ceil()对比横评

    首先,这些处理方法可分为三类. 1,只用来处理数字取整问题的:Math.round(),Math.floor(),Math.ceil(): 2,专门用于把字符串转化成数值:parseInt(),par ...

  3. js 中的 Math.ceil() Math.floor Math.round()

    alert(Math.ceil(25.9)); alert(Math.ceil(25.5)); alert(Math.ceil(25.1)); alert(Math.round(25.9)); ale ...

  4. 如何在一个页面后面随机跳转到多个链接地址Math.floor()和Math.random()

    点击一个标签随机跳转到多个链接地址,主要运用javascript中的Math.floor()和Math.random()方法 floor(x) 方法是向下去整数 参数为任意数值或表达式. floor( ...

  5. Math.round()、Math.ceil()、Math.floor()与Math.random()的区别?

    Math.round(x) 四舍五入 加上0.5向下取整 Math.round(1.5) 2 Math.round(-11.5) -11 Math.round(-11.2) -10 Math.ceil ...

  6. 转载:使用Math.floor和Math.random取随机整数

    Math.random():获取0~1随机数 Math.floor() method rounds a number DOWNWARDS to the nearest integer, and ret ...

  7. JavaScript中的内置对象-8--3.Math-Math对象的方法-min()- max()- ceil() - floor()- round()- abs(); Math对象的random()方法;

    JavaScript内置对象-3.Math(数值) 学习目标 1.掌握Math对象的方法: min() max() ceil() floor() round() abs() Math.min() 语法 ...

  8. Python标准库12 数学与随机数 (math包,random包)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们已经在Python运算中看到Python最基本的数学运算功能.此外,math包 ...

  9. Java Math floor round ceil 函数

    public final class Math extends Object public static double floor(double a) public static long round ...

随机推荐

  1. C#相等性 - 三个方法和一个接口

    简介 C#(.NET)的object类里面有三个关于判断相等性的方法: public virtual bool Equals(object obj) public static bool Equals ...

  2. .NET Core IdentityServer4实战-开篇介绍与规划

    一.开篇寄语 由于假期的无聊,我决定了一个非常有挑战性的活动,也就是在年假给大家带来一个基于OAuth 2.0的身份授权框架,它就是 IdentityServer4 ,如果没有意外的话,一定可以顺利的 ...

  3. 依赖注入容器-- Autofac

    目录: 一.简介 二.如何使用 2.1.基本使用 2.2.接口使用 2.3. 其他注入 2.4. 注入的生命周期 一.简介 在上一篇文章中讲到替换默认服务容器,我们选择了Autofac Autofac ...

  4. Python 中的设计模式详解之:策略模式

    虽然设计模式与语言无关,但这并不意味着每一个模式都能在每一门语言中使用.<设计模式:可复用面向对象软件的基础>一书中有 23 个模式,其中有 16 个在动态语言中“不见了,或者简化了”. ...

  5. Java基础系列-ArrayList

    原创文章,转载请标注出处:<Java基础系列-ArrayList> 一.概述 ArrayList底层使用的是数组.是List的可变数组实现,这里的可变是针对List而言,而不是底层数组. ...

  6. Restframe_work 回顾记忆集

    目录 Restframe_work 回顾记忆集 rest_framework主要功能介绍 rest_framework主要模块介绍 记忆集 错题集 混淆集 重点集 难点集 Restframe_work ...

  7. html基础复习

    1.标签选择器   div{}   2.类选择器   .one class="one"   3.id选择器(定义+调用)   #one{} id="one"   ...

  8. 【技术讨论】RF环境搭建手册

    (原创文章,转载请注明出处.) 简要整理下环境搭建的步骤,以便快速.准确的搭建测试环境. 一.环境搭建 一.Python 2.7 1. 不要用Python3.6,很多库3.6中还没有,wxPython ...

  9. Windows 10-限制Windows更新上传带宽

    Windows Update Delivery Optimization可帮助您更快,更可靠地获取Windows更新和Microsoft Store应用程序. Windows Update Deliv ...

  10. 利用requirejs实现vue的模块化开发

    通常vue都是搭配webpack+vue-cli使用的 如果不在nodejs环境下开发web应用呢? 这里提出一个解决方案: 1.加载requirejs,并且指定main函数 <script d ...