


Summary of several methods for generating random numbers in javascript
Jan 17, 2018 am 10:37 AMThis 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)); }
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; }
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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

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

Simple JavaScript Tutorial: How to Get HTTP Status Code
