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的随机整数了.

转自:https://www.cnblogs.com/rexmzk/archive/2012/03/07/2384409.html

转载:使用Math.floor和Math.random取随机整数的更多相关文章

  1. Math.random取随机整数

    Math.random可以随机获取0-1的数字,今天用的需要给id随机赋值,小数不好控制,就只取整. 网上很多是 int i=(int)(Math.random()*100): 报错: 后找到 var ...

  2. 【转载】Javascript使用Math.floor方法向下取整

    在Javascript的数值运算中,很多时候需要对最后计算结果向下取整,Math.floor是javascript中对计算结果向下取整的函数,它总是将数值向下舍入为最接近的整数.此外Math.ceil ...

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

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

  4. js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结

    Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数,具体的区别请看下面的总结. 一.Math.round 作用:四舍五入,返回参数+0.5后,向下取整 ...

  5. js中Math.round、parseInt、Math.floor和Math.ceil小数取整小结

    以前经常在代码中看到Math.round.parseInt.Math.floor和Math.ceil这四个函数,虽然知道结果都可以返回一个整数,但是对他们四者的区别还是不太清楚,今天就做一个小结. 一 ...

  6. js中Math.round、parseInt、Math.floor和Math.ceil小数取整小结【转】

    [摘要:之前常常正在代码中看到Math.round.parseInt.Math.floor战Math.ceil那四个函数,固然晓得效果皆能够返回一个整数,然则对他们四者的差别照样没有太清晰,本日便做一 ...

  7. js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结(转)

    js中Math.round.parseInt.Math.floor和Math.ceil小数取整总结 Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数 ...

  8. JavaScipt中的Math.ceil() 、Math.floor() 、Math.round()、Math.pow() 三个函数的理解

    以前一直会三个函数的使用产生混淆,现在通过对三个函数的原型定义的理解,其实很容易记住三个函数. 现在做一个总结: 1. Math.ceil()用作向上取整. 2. Math.floor()用作向下取整 ...

  9. Math.ceil()、Math.floor()和Math.round()

    下面来介绍将小数值舍入为整数的几个方法:Math.ceil().Math.floor()和Math.round(). 这三个方法分别遵循下列舍入规则: Math.ceil()执行向上舍入,即它总是将数 ...

随机推荐

  1. .net mvc 利用分部视图局部刷新.

    页面利于$.Ajax: $(function(){ $("#btnpartview").click(function () { var model = []; model.push ...

  2. 虚拟机Vmware成功安装Ubuntu Server 16.04中文版

    最近想在Linux下学习Python的爬虫开发技术,经过认真考虑优先选择在在Ubuntu环境下进行学习Python的开发,虽然Ubuntu Server 16.04 LTS版本已经集成了Python ...

  3. python小练习之三---购物车程序

    购物车购物的例子 严格来讲,这个例子相对大一些 功能也稍完备一些,具有用户登录,商品上架,用户购物,放入购物车,展示每个用户的购物车里的商品的数量,用户账户余额,支持用户账户充值等 下面展示的代码有些 ...

  4. jenkins创建multibranch pipeline

    参考以下文章进行实践: https://jenkins.io/doc/pipeline/tour/hello-world/#what-is-a-jenkins-pipeline (看见一个介绍的还不错 ...

  5. Python/Django(CBV/FBV/ORM操作)

    Python/Django(CBV/FBV/ORM操作) CBV:url对应的类(模式) ##====================================CBV操作============ ...

  6. HDU-1850 Being a Good Boy in Spring Festival---尼姆博奕的运用

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1850 题目大意: 中文题: 思路: 传送门:尼姆博奕 #include<iostream> ...

  7. Downloader Middleware

    downloader middleware作用:改写请求,如加入代理,加入头部等:处理异常等. # downloadMiddleware举例, 加代理 # 这里的类如果已经重新设置名称,记得在sett ...

  8. logging格式

    import logging def foo(s): return 10 / int(s) def bar(s): return foo(s) * 2 def main(): try: bar(0) ...

  9. winform 如何加载Url图像(图像)

    解决方法: 1pictureBox1.Image = Image.FromStream(System.Net.WebRequest.Create(http://www.baidu/new.gif ). ...

  10. ACE入门——ACE构建

    ACE(ADAPTIVE Communication Environment),ACE入门的第一课就是要学习怎么在自己的系统上构建ACE. ACE是跨平台的,这是它的一个很重要的特性,ACE支持很多的 ...