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

韦小宝
韦小宝Original
2018-01-17 10:37:222834browse

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));  
}

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!

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