Home  >  Article  >  Web Front-end  >  Does the return value of random method in javascript contain 0?

Does the return value of random method in javascript contain 0?

WBOY
WBOYOriginal
2022-03-02 16:33:232537browse

In JavaScript, the return value of the random() method contains 0; the random() method can return a random number between 0 and 1. The returned result does not contain 1, but contains 0. The syntax is "Math .random()".

Does the return value of random method in javascript contain 0?

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

Does the return value of the random method in JavaScript include 0?

The random() method can return a random number between 0 (inclusive) ~ 1 (exclusive).

The syntax is:

Math.random()

Return value:

A pseudo-random number between 0.0 ~ 1.0 (not included).

The example is as follows:

<html>
<head>
<meta charset="utf-8">
<title>123</title>
</head>
<body>
<p id="demo">点击按钮显示一个 1 到 100 的随机数。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
var x=document.getElementById("demo")
x.innerHTML=Math.floor((Math.random()*100)+1);
}
</script>
</body>
</html>

Output result:

Does the return value of random method in javascript contain 0?

Related recommendations: javascript learning tutorial

The above is the detailed content of Does the return value of random method in javascript contain 0?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn