Home Web Front-end JS Tutorial How to use random() function

How to use random() function

Feb 14, 2019 am 10:47 AM
random

The random() function can be used to return a floating-point pseudo-random number in the range 0 (inclusive) to 1 (exclusive). This random number can then be scaled according to the required range. Let's take a look at the specific use of the random() function.

How to use random() function

Let’s first take a look at the basic syntax of the random() function

Math.random()
Copy after login

math.random() function return range is [ 0,1).

Let’s look at a specific example

Get a random number between [0,1)

The code is as follows

<!DOCTYPE html>
<html>
<head>
   <title></title>
</head>
<body>
<script type="text/javascript"> 
    var random = Math.random( ); 
    document.write("生成的随机数 : " + random );  
</script> 
</body>
</html>
Copy after login

The running result is:

生成的随机数 : 0.8336114321997108
Copy after login

Get a random number between two values: Math.random() can be used to get a random number between two values. The return value is [min,max).

The code is as follows

<!DOCTYPE html>
<html>
<head>
   <title></title>
</head>
<body>
<script type="text/javascript"> 
    var min=4; 
    var max=5;  
    var random = Math.random() * (+max - +min) + +min; 
    document.write("生成的随机数 : " + random );  
</script> 
</body>
</html>
Copy after login

The running result is:

生成的随机数 : 4.887121143160121
Copy after login

Get a random integer between two values: Math.random() Can be used to get an integer between two values. If min is not an integer, the return value is not less than min or the next integer greater than min, and is less than but not equal to max.

The code is as follows

<!DOCTYPE html>
<html>
<head>
   <title></title>
</head>
<body>
<script type="text/javascript"> 
    var min=4; 
    var max=8;  
    var random =Math.floor(Math.random() * (+max - +min)) + +min; 
    document.write("生成的随机数 : " + random );  
</script> 
</body>
</html>
Copy after login

The running result is:

生成的随机数 : 6
Copy after login

This article ends here. For more exciting content, you can pay attention to other related columns of the php Chinese website Tutorial! ! !

The above is the detailed content of How to use random() function. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use demo of python random library How to use demo of python random library May 05, 2023 pm 08:13 PM

Simple use of pythonrandom library demo When we need to generate random numbers or randomly select elements from a sequence, we can use Python's built-in random library. The following is an annotated example that demonstrates how to use the random library: #Import random library importrandom #Generate a random decimal between 0 and 1 random_float=random.random()print(random_float)#Generate a random decimal within the specified range Random integer (including endpoints) random_int=random.randint(1,10)print(random_int)#

Java generates random double precision floating point numbers using nextDouble() function of Random class Java generates random double precision floating point numbers using nextDouble() function of Random class Jul 25, 2023 am 09:06 AM

Java uses the nextDouble() function of the Random class to generate random double-precision floating-point numbers. The Random class in Java is a pseudo-random number generator that can be used to generate different types of random numbers. Among them, the nextDouble() function is used to generate a random double-precision floating point number. Before using the Random class, we need to import the java.util package first. Next we can create a Random object and use the nextDouble() function

How to use the random.choice() function to randomly select an element from a list in Python 2.x How to use the random.choice() function to randomly select an element from a list in Python 2.x Aug 01, 2023 pm 12:25 PM

Python is a very popular programming language, and its random module provides various random number generation functions. This article will introduce how to use the random.choice() function in Python2.x to randomly select an element from a list. In Python, the random module is one of the core modules that implements random number generation. It provides various methods for generating random numbers, including generating random integers, random floating point numbers, random selection, etc. In our case we use r

How to use Random.nextInt() method to generate random numbers in Java? How to use Random.nextInt() method to generate random numbers in Java? Nov 18, 2023 pm 03:44 PM

How to use Random.nextInt() method to generate random numbers in Java? Random numbers are widely used in computer science and can be used to generate passwords, random events in games, random sampling in data science, etc. Java provides the Random class to generate random numbers, and the nextInt() method can be used to generate a random integer. Below I will introduce how to use the Random.nextInt() method to generate random numbers and provide specific code examples. First, we need

How to use random function in C language How to use random function in C language Sep 05, 2023 am 10:49 AM

Usage of random function in C language: 1. random.random, randomly generates a floating point number between (0,1); 2. random.randint, randomly generates an integer within the range, and the two parameters represent the upper limit and lower limit respectively; 3 , random.randrange, within the specified range, obtain a random number from the set incremented by the specified base; 4. random.choice, randomly select a number from the sequence; 5. random.shuffle, randomly sort.

How to generate random numbers using Random function in Java How to generate random numbers using Random function in Java Jun 26, 2023 pm 02:55 PM

When writing Java programs, we often need to generate random numbers for various purposes, such as test data, password generation, etc. Java provides the Random class to implement the function of generating random numbers. This article will introduce how to use the Random function in Java to generate random numbers. Import the Random class Before using the Random class, we need to import it, which can be imported using the import statement at the beginning of the code. The sample code is as follows: importjava.util.Random;

How to use the random.choice() function to randomly select an element from a list in Python 3.x How to use the random.choice() function to randomly select an element from a list in Python 3.x Jul 30, 2023 pm 03:57 PM

How to use the random.choice() function in Python3.x to randomly select an element from a list. Randomly selecting an element is a very common requirement in programming. The random module in Python3.x provides a very convenient function random.choice() to implement this function. It randomly selects an element from the given list and returns it. Below I will introduce how to use the random.choice() function and provide some code examples.

How to solve the seed problem in Java Random class How to solve the seed problem in Java Random class May 03, 2023 pm 07:37 PM

It can be said that in current computer languages ​​and ordinary users, there is no way to obtain true random numbers. True random numbers are now only available in quantum computers. So the random numbers we are talking about now can also be called pseudo-random numbers. Pseudo-random numbers are the results obtained through an algorithm and combined with the next regularly changing number (such as time). We call this data that is collected through the algorithm time as a basis for initially obtaining random numbers as a seed. There are two ways to create random numbers in Java: the first is to directly create a Random object Randomrandom=newRandom(); this method will be called at the bottom to get the seed: the second is to create it yourself

See all articles