This time I will show you how to generate random numbers with JS, and what are the precautions for generating random numbers with JS. The following is a practical case, let’s take a look.
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 <p style="text-align: left;">1.Math.random(); The result is a random number between 0-1 (including 0, excluding 1)</p><p style="text-align: left;">2.Math.floor(num); Parameter num is a numerical value, and the function result is the integer part of num. </p><p style="text-align: left;">3.Math.round(num); The parameter num is a numerical value, and the function result is the integer after num is rounded. </p><p style="text-align: left;">Math: Mathematical object, providing mathematical calculations on data. </p><p style="text-align: left;">Math.random(); Returns a random number between 0 and 1 (including 0, excluding 1). </p><p style="text-align: left;">Math.ceil(n); Returns the smallest integer greater than or equal to n. </p><p style="text-align: left;">When using Math.ceil(Math.random()*10);, you mainly get random integers from 1 to 10, and the probability of getting 0 is very small. </p><p style="text-align: left;">Math.round(n); Returns the value of n after rounding. </p><p style="text-align: left;">Use Math.round(Math.random()); to obtain a balanced random integer from 0 to 1. </p><p style="text-align: left;">When using Math.round(Math.random()*10);, you can obtain random integers from 0 to 10 in a basically balanced manner, and the probability of obtaining the minimum value 0 and the maximum value 10 is less than half. </p><p style="text-align: left;">Math.floor(n); Returns the largest integer less than or equal to n. </p><p style="text-align: left;">When using Math.floor(Math.random()*10);, random integers from 0 to 9 can be obtained evenly. <br>Random example of random function for js to generate random numbers</p><p style="text-align: left;"><span style="color: #ff0000"><strong>JavaScript Math.random() built-in function</strong></span></p><pre class="brush:php;toolbar:false">random函数返回值 返回0和1之间的伪随机数,可能为0,但总是小于1,[0,1) random函数示例 //返回随机数 document.write(Math.random()); //返回10-20的随机数 document.write(Math.random()*(20-10)+10); //返回指定范围的随机数(m-n之间)的公式 document.write(Math.random()*(n-m)+m);
Based on time, it can also be The code to generate a random number
is as follows:
var now=new Date(); var number = now.getSeconds(); //这将产生一个基于目前时间的0到59的整数。 var now=new Date(); var number = now.getSeconds()%43; //这将产生一个基于目前时间的0到42的整数。
Js The code to generate a 6-digit random number
is as follows:
<script> function MathRand() { var Num=""; for(var i=0;i<6;i++) { Num+=Math.floor(Math.random()*10); } document.getElementById("Lb_Random").innerText=Num; document.getElementById("Lb_Random").innerHTML=Num; } </script>
Multiple ways to generate random strings with JS
The code is as follows:
<script> function randomString(len) { len = len || 32; var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/ var maxPos = $chars.length; var pwd = ''; for (i = 0; i < len; i++) { pwd += $chars.charAt(Math.floor(Math.random() * maxPos)); } return pwd; } document.write(randomString(32)); </script>
It goes without saying how to use it, Call the randomString method, and the parameter len is the length of the returned random string.
The parameter passed is the length. If there is no parameter, the default output is 32 characters.
Several uses of JS to generate random numbers!
The code is as follows:
<script> function GetRandomNum(Min,Max) { var Range = Max - Min; var Rand = Math.random(); return(Min + Math.round(Rand * Range)); } var num = GetRandomNum(1,10); alert(num); </script> 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 <p style="text-align: left;">1.Math.random(); The result is a random number between 0-1 (including 0, excluding 1) <br>2.Math.floor(num); The parameter num is a numerical value, and the function result is the integer part of num. <br>3.Math.round(num); The parameter num is a numerical value, and the function result is the integer after num is rounded. </p><p style="text-align: left;">Math: Mathematical object, providing mathematical calculations on data. <br>Math.random(); Returns a random number between 0 and 1 (including 0, excluding 1). </p><p style="text-align: left;">Math.ceil(n); Returns the smallest integer greater than or equal to n. <br>When using Math.ceil(Math.random()*10);, you mainly get random integers from 1 to 10, and the probability of getting 0 is very small. </p><p style="text-align: left;">Math.round(n); Returns the value of n after rounding. <br>Use Math.round(Math.random()); to obtain a random integer from 0 to 1 evenly. <br>When using Math.round(Math.random()*10);, random integers from 0 to 10 can be obtained in a basically balanced manner, and the probability of obtaining the minimum value 0 and the maximum value 10 is less than half. </p><p style="text-align: left;">Math.floor(n); Returns the largest integer less than or equal to n. <br>When using Math.floor(Math.random()*10);, random integers from 0 to 9 can be obtained evenly. </p><p style="text-align: left;"><span style="color: #ff0000"><strong>js generates a random string timestamp to obtain</strong></span></p><p style="text-align: left;">The default JS generated is 13 digits, which needs to be passed to php/1000</p><p style="text-align: left;">The code is as follows:</p><pre class="brush:php;toolbar:false">timestamp = timestamp/1000; <script> function randomChar(l) { var x="0123456789qwertyuioplkjhgfdsazxcvbnm"; var tmp=""; var timestamp = new Date().getTime(); for(var i=0;i< l;i++) { tmp += x.charAt(Math.ceil(Math.random()*100000000)%x.length); } return timestamp+tmp;</script>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to make infinite loading data requirements at the bottom of the sliding page
##Using vue-devtools from scratch
The above is the detailed content of How to generate random numbers in JS. For more information, please follow other related articles on the PHP Chinese website!

如何使用JS和百度地图实现地图平移功能百度地图是一款广泛使用的地图服务平台,在Web开发中经常用于展示地理信息、定位等功能。本文将介绍如何使用JS和百度地图API实现地图平移功能,并提供具体的代码示例。一、准备工作使用百度地图API前,首先需要在百度地图开放平台(http://lbsyun.baidu.com/)上申请一个开发者账号,并创建一个应用。创建完成

如何使用JS和百度地图实现地图多边形绘制功能在现代网页开发中,地图应用已经成为常见的功能之一。而地图上绘制多边形,可以帮助我们将特定区域进行标记,方便用户进行查看和分析。本文将介绍如何使用JS和百度地图API实现地图多边形绘制功能,并提供具体的代码示例。首先,我们需要引入百度地图API。可以利用以下代码在HTML文件中导入百度地图API的JavaScript

js字符串转数组的方法:1、使用“split()”方法,可以根据指定的分隔符将字符串分割成数组元素;2、使用“Array.from()”方法,可以将可迭代对象或类数组对象转换成真正的数组;3、使用for循环遍历,将每个字符依次添加到数组中;4、使用“Array.split()”方法,通过调用“Array.prototype.forEach()”将一个字符串拆分成数组的快捷方式。

如何使用JS和百度地图实现地图热力图功能简介:随着互联网和移动设备的迅速发展,地图成为了一种普遍的应用场景。而热力图作为一种可视化的展示方式,能够帮助我们更直观地了解数据的分布情况。本文将介绍如何使用JS和百度地图API来实现地图热力图的功能,并提供具体的代码示例。准备工作:在开始之前,你需要准备以下事项:一个百度开发者账号,并创建一个应用,获取到相应的AP

js中new操作符做了:1、创建一个空对象,这个新对象将成为函数的实例;2、将新对象的原型链接到构造函数的原型对象,这样新对象就可以访问构造函数原型对象中定义的属性和方法;3、将构造函数的作用域赋给新对象,这样新对象就可以通过this关键字来引用构造函数中的属性和方法;4、执行构造函数中的代码,构造函数中的代码将用于初始化新对象的属性和方法;5、如果构造函数中没有返回等等。

这篇文章主要为大家详细介绍了js实现打字小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

php在特定情况下可以读js内部的数组。其方法是:1、在JavaScript中,创建一个包含需要传递给PHP的数组的变量;2、使用Ajax技术将该数组发送给PHP脚本。可以使用原生的JavaScript代码或者使用基于Ajax的JavaScript库如jQuery等;3、在PHP脚本中,接收传递过来的数组数据,并进行相应的处理即可。

js全称JavaScript,是一种具有函数优先的轻量级,直译式、解释型或即时编译型的高级编程语言,是一种属于网络的高级脚本语言;JavaScript基于原型编程、多范式的动态脚本语言,并且支持面向对象、命令式和声明式,如函数式编程。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
