js Math.rander的用法
文章目录
- 前言
- 一、基础随机数求法
- 二、基础用法扩展
- 三、再扩展
前言
本文展示了求js随机数的一些常用用法
–
一、基础随机数求法
获取一个 [0,1) 的随机数
var random1 = Math.random()console.log(random1); //0.15502465163041257
Math.random()方法 得出的是一个大于或者等于0 然后最大可以无限接近于1 不等于1的随机值
所有扩展用法都是基于这个取值范围得到的
二、基础用法扩展
生成 [one, two) 范围内的随机数
function getRandom1(one, two) {if (one < two) {return Math.random() * (two - one) + one} else {return Math.random() * (one - two) + two}}console.log(getRandom1(2, 4), '<'); // 3.021933876110561 '<'console.log(getRandom1(4, 2), '>'); // 3.2829077427720192 '>'
生成 (one, two) 范围内的随机数
function getRandom2(one, two) {if (one < two) {var data = Math.random() * (two - one) + onewhile (data !== one) {return data}return getRandom2(one, two)} else {var data = Math.random() * (one - two) + twowhile (data !== one) {return data}return getRandom2(one, two)}}console.log(getRandom2(2, 3), '()'); // 2.684042669080733 '()'
三、再扩展
生成指定位数的随机数
代码如下(示例):
function random3(n) {let Begin = ''for (let i = 0; i < n; i++) {Begin += Math.floor(Math.random() * 10)}let result = Number(Begin)return result}console.log(random3(9), 'DIYnumber'); // 735140887 'DIYnumber'
可以结合 Math 其他的一些方法 对得到的随机值进行取整等操作
js Math.rander的用法相关推荐
- js Math用法jquery是否为空对象判断
Js ◎Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数: ◎Math.floor()执行向下舍入,即它总是将数值向下舍入为最接近的整数: ◎Math.round()执行标准舍 ...
- javascript基础——文字变大变小className的使用及JS浮动的兼容用法
文字变大变小,详情页比较常见的那种 <!DOCTYPE html> <html lang="en"> <head><meta charse ...
- boost::math::tools::polynomial用法的测试程序
boost::math::tools::polynomial用法的测试程序 实现功能 C++实现代码 实现功能 boost::math::tools::polynomial用法的测试程序 C++实现代 ...
- boost::math::tools::sum_series用法的测试程序
boost::math::tools::sum_series用法的测试程序 实现功能 C++实现代码 实现功能 boost::math::tools::sum_series用法的测试程序 C++实现代 ...
- boost::math::tools::simple_continued_fraction用法的测试程序
boost::math::tools::simple_continued_fraction用法的测试程序 实现功能 C++实现代码 实现功能 boost::math::tools::simple_co ...
- boost::math::quadrature::trapezoidal用法的测试程序
boost::math::quadrature::trapezoidal用法的测试程序 实现功能 C++实现代码 实现功能 boost::math::quadrature::trapezoidal用法 ...
- boost::math::boost::math::interpolators::cardinal_cubic_b_spline用法的测试程序
boost::math::boost::math::interpolators::cardinal_cubic_b_spline用法的测试程序 实现功能 C++实现代码 实现功能 boost::mat ...
- boost::math::filters::daubechies_scaling_filter用法的测试程序
boost::math::filters::daubechies_scaling_filter用法的测试程序 实现功能 C++实现代码 实现功能 boost::math::filters::daube ...
- boost::math::quadrature::exp_sinh用法的测试程序
boost::math::quadrature::exp_sinh用法的测试程序 实现功能 C++实现代码 实现功能 boost::math::quadrature::exp_sinh用法的测试程序 ...
- boost::math::quadrature::gauss用法的测试程序
boost::math::quadrature::gauss用法的测试程序 实现功能 C++实现代码 实现功能 boost::math::quadrature::gauss用法的测试程序 C++实现代 ...
最新文章
- HDU 4333 [SAM WRONG!!!]
- java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Integer
- 计算机背景象怎样改变,我的页面背景总是黑色,以前改变过现在好象改变不了?我该怎么改变呢? 爱问知识人...
- cesium 经纬度绘制点_cesium结合geoserver利用WFS服务实现图层新增(附源码下载)
- 精简改良(生成树dp)
- 数据结构与算法 | 二叉树的实现
- html怎么在图片上添加文字_Image J基础操作:给图片添加文字和标注
- 网站克隆工具_Kali Linux工具篇十三:网站克隆技巧Httrack使用技巧
- 博士毕业要发多少篇文章? 72 所高校大比较,发文最多的是……
- 文章/网站分享工具——百度分享
- 择校秘籍|保研去 北大软微 还是 中科院计算所?
- 【Cubieboard2】配置编译内核支持SPI全双工通信驱动
- VS Code下载Beautify扩展插件 | CSDN创作打卡
- s_p_a_r_k_内核
- Unity发布的exe程序正常 显示窗口右上角的放大缩小功能
- html 3D球状旋转标签云文字云效果
- ROS键盘控制机器人
- 艾德莱斯绸:“千年时尚”托起新产业
- 红帽 linux 更换主板,RedHatEnterpriseLinux7更换CentOS7yum源
- 直线检测论文(霍夫变换,LSD,DWP,L-CNN,AFM,HAWP,PPGNet,TP-LSD,M-LSD,LETR)
热门文章
- JS中this的四种调用方法
- matlab求含参数一元三次方程,matlab 求解一元三次方程,带其他参数
- linux解压下载的文件,Linux下各种压缩文件的解压方法
- 【Java基础】ArraylistLinkedList机制,failfastfailsafe
- 一般现在时,主语为第三人称单数,例句说明
- 微信淘宝客查券返利机器人搭建教程分享
- Scrum板与Kanban如何抉择?敏捷工具:dkijbh板与按照lfhgvvvh
- 成功解决台式电脑或者笔记本电脑右下角音量、电池等图标消失等问题
- 投稿Springer旗下某中科院1区TOP期刊时间记载
- 植物病虫害识别方法主要研究思路