Write a JS function that has an n (numeric type) whose return value is an array of n random and non-repeating integers, and the integer value range is [2,32]

Source: Internet
Author: User

Today in the public number to see this problem, I wrote down, found himself still have a lot of unnoticed, in this record,

Returns a random integer function getrandom (min, max) {var rand = Math.Round (Math.random () * (max-min) + min); return rand;}; repeatability validation function isrepeat (arr,n) {if (Arr.indexof (n) >-1) {return true;}; return false;}; function fn (n) {var arr = [],max = 32,min = 2;//is the verification of the numbers var isnum =!isnan (number (n));//Whether it is legally validated var Randok = (n >= 1 && N <= (max-min + 1))? True:false; if (isnum && Randok) {for (var i = 0; i < n; i++) {var rand = Getrandom (Min,max); if (Isrepeat (Arr,rand)) {i -- ; }else{Arr.push (rand);}} } return arr; }; Console.log (FN (31)) encountered several problems in this process, about the function take the integer problem to do the collation.

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 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 after num is rounded.

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), a random integer of 1 to 10 is obtained, with a minimum probability of 0 being taken.

Math.Round (n); Returns the value of an integer after n rounding.
With Math.Round (Math.random ()), a random integer from 0 to 1 can be obtained evenly.
With Math.Round (Math.random () *10), a basic equalization can be obtained for random integers from 0 to 10, where the probability of obtaining a minimum of 0 and a maximum of 10 is less than half.

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

Write a JS function that has an n (numeric type) whose return value is an array of n random and non-repeating integers, and the integer value range is [2,32]

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.