Home > Web Front-end > JS Tutorial > body text

Sharing examples of generating random numbers using javascript

小云云
Release: 2018-01-31 14:39:34
Original
1331 people have browsed it

This article mainly introduces relevant information summarized by several methods of generating random numbers in JavaScript. I hope that through this article, everyone can master how to implement such a method. Friends who need it can refer to it. I hope it can help everyone.

javascript Summary of 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. Mixing 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.Instructions

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

Math: Mathematical object, providing 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.

Related recommendations:

JavaScript random number generator implementation method

How to implement MySQL to generate random numbers and connect strings

php rand() function for random number generation

The above is the detailed content of Sharing examples of generating random numbers using javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!