Fn javscript a write function, a function with a parameter n (digital type), the return value is an array within the array is non-repeating n random integer, and is an integer in the range [2, 32].

Fn a first defined to return an integer in the range:

function getRand(a,b){
    var rand = Math.ceil(Math.random()*(b-a)+a);
    return rand;
}

To write a func used for recalibration:

Copy the code
function checkArrIn(rand,array){
    if(array.indexOf(rand) > -1){
        return true
    }
    return false;
}
Copy the code

Finally, the overall implementation:

Copy the code
Fn function (n, min, max) { 
    var ARR = []; 
    var scanned using the ISNUM isNaN = (Number The (n));! // is not determined a number n, containing string-type Digital 
    var isRandOk = (n> = ? min && n <= max && n <= (max - min)) true: false; // the value of n is determined to meet the requirements 
    iF (n && && isRandOk scanned using the ISNUM) { 
        for (var I = 0; I <n ; I ++) { 
            var = RAND the getRand (min, max); 
            IF (checkArrIn (RAND, ARR)) { 
                i-- 
            } the else { 
                arr.push (RAND) 
            } 
        } 
    } 
    return ARR; 
}

Guess you like

Origin www.cnblogs.com/SallyShan/p/11465093.html