The usage of generating random numbers in JS and the _javascript techniques of correlation functions

Source: Internet
Author: User
Tags numeric value rand

Let me first introduce you to some related functions of random numbers:
var Rand = Math.random ();

    • 1.math.random (); The result is a random number between 0-1 (including 0, excluding 1)
    • 2.math.floor (num); Parameter num is a numeric value, and the function result is the integer portion of Num.
    • 3.math.round (num); The parameter num is a numeric value, and the function result is the integer rounded by Num.

Math : A Mathematical object that provides a mathematical calculation of the 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.
With Math.ceil (Math.random () *10); , the number of random integers from 1 to 10 is mostly obtained, and the probability of taking 0 is minimal.

Math.Round (n); Returns the value of the integer after n rounding.
With Math.Round (Math.random ()); A random integer of 0 to 1 can be obtained evenly.
With Math.Round (Math.random () *10); , a random integer of 0 to 10 can be basically balanced, with half the probability of obtaining a minimum value of 0 and a maximum of 10.

Math.floor (n); Returns the largest integer less than or equal to N.
With Math.floor (Math.random () *10); , a random integer of 0 to 9 can be obtained evenly.
Random draw, can also be extended, such as the probability of setting a lottery, and the use of the database.

Winning probability demand, 100% jackpot, there are 3 prizes, but the probability of drawing a book for 20%
function Draw () {
  var d_s = Getrandom ();
  if (d_s >= 1 && d_s <=) {
    alert (' Congratulations on your smoking xxx! ');
  } else if (d_s >= && d_s <=) {
    alert (' Congratulations on your smoking xxx! ');
  } else {
    alert (' Congratulations on your drawing of the book! ');
  }
}

Corresponding JS generates random number of function code:

<script> 
 2function getrandomnum (Min,max)
{  
  var Range = max-min;  
  var Rand = Math.random ();  
  Return (Min + math.round (Rand * Range));  
 8var num = getrandomnum (1,10);  
 9alert (num);  
</script>

var chars = [' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ' , ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ';

function generatemixed (n) {
   var res = "";
   for (var i = 0; i < n; i + +) {
     var id = math.ceil (math.random () *35);
     Res + + Chars[id];
   }
   return res;
}

The above is the detailed content of this article, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.