Home Web Front-end JS Tutorial Summary of several methods for generating random numbers in javascript

Summary of several methods for generating random numbers in javascript

Jan 17, 2018 am 10:37 AM
javascript js random number

This article mainly introduces javascript related information summarized by several methods of generating random numbers. I hope that through this article you can master how to implement such methods in JavaScript. Friends who are interested in JavaScript can refer to the following This article

javascript summarizes several methods of generating random numbers

1. Get a random number between two numbers

function GetRandomNum(Min,Max){  
  var Range = Max - Min;  
  var Rand = Math.random();  
  return(Min + Math.round(Rand * Range));  
}
Copy after login

2, Mixed method

function generateMixed(n) {
   var res = "";
   for(var i = 0; i < n ; i ++) {
     var id = Math.ceil(Math.random()*35);
     res += chars[id];
   }
   return res;
}
Copy after login

3, Description

1.Math.random(); The result is 0 A random number between -1 (including 0, excluding 1)
2.Math.floor(num); The parameter num is a numerical value, FunctionThe result is the integer of numpart.
3.Math.round(num); The parameter num is a numerical value, and the function result is the integer after num is rounded.

Math: MathObject, provides mathematical calculations on data.
Math.random(); Returns a random number between 0 and 1 (including 0, excluding 1).

Math.ceil(n); Returns the smallest integer greater than or equal to n.
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.

Math.round(n); Returns the value of n after rounding.
Use Math.round(Math.random()); to obtain a random integer from 0 to 1 evenly.
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.

Math.floor(n); Returns the largest integer less than or equal to n.
When using Math.floor(Math.random()*10);, random integers from 0 to 9 can be obtained evenly.

If you have any questions, please leave a message or go to the community of this site to communicate and discuss. Thank you for reading. I hope it can help everyone. Thank you for your support of this site!

Related recommendations:

Three implementation methods of JavaScript defined functions

Instances of javascript calculating gradient colors

A brief discussion on the difference between Ajax and JavaScript

The above is the detailed content of Summary of several methods for generating random numbers in javascript. 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 Article

Hot Article

Hot Article Tags

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 implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to implement an online speech recognition system using WebSocket and JavaScript

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Recommended: Excellent JS open source face detection and recognition project

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecasting system

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

Simple JavaScript Tutorial: How to Get HTTP Status Code

The relationship between js and vue The relationship between js and vue Mar 11, 2024 pm 05:21 PM

The relationship between js and vue

See all articles