Automatic random page Jump after JavaScript opens the page

Keywords: Database

Hi, I've met again. I met a problem in the latest demand. I'd like to summarize here, and I hope to exchange and learn with you;

1, Project requirements

At the end of the year and the beginning of the year, the company's app needs to launch multiple activities and create an aggregation page. When entering the aggregation page from the external link, it will randomly jump to the activity page;

2, Demand analysis
At that time, two directions were considered: one is to realize random jump in js; the other is to store the link in the database and return to the front desk through the query link for random jump. Finally, I used the first one, because it is convenient, it may be a little unsafe, so don't spray;

3, Solutions
In the actual development, I found that using random number to jump, before and after two times into the aggregation page random jump into the same page probability is very large;

In the following solution, two solutions are provided to solve the problem that random jump does not repeat before and after entering the aggregate page twice;


//Store the random number of the first entry
var globalNum = window.sessionStorage.getItem("skipNum", skipNum);


$(function(){

    /**
     * First option
     * 
     */
    //Get random number
    var  luckyNum = Math.round(Math.random()*3);
    //alert(luckyNum);
    if(luckyNum==0){
        window.location.href = "";
    }else if(luckyNum==1){
        window.location.href = "";
    }else if(luckyNum==2){
        window.location.href = "";
    }else if(luckyNum==3){
        window.location.href = "";
    }
    /**
     * Second option
     * 
     */
    var arr = [1,2,3,4];
    var result = [];
        if(arr.length ===0 ){
            arr=result;
            result = [];
        }
        var num = arr.splice(Math.floor(Math.random()*arr.length),1);
        result = num.concat(result);
        alert(result[0]);
        var  luckyNum = result[0];
        //alert(luckyNum);
        if(luckyNum==1){
            window.location.href = "";
        }else if(luckyNum==2){
            window.location.href = "";
        }else if(luckyNum==3){
            window.location.href = "";
        }else if(luckyNum==4){
            window.location.href = "";
        }

    /**
     * Third option
     */
        var count=4;
        var originalArray=new Array;//Original array 
        //Assign value to original array originalArray 
        for (var i=0;i<count;i++){ 
        originalArray[i]=i+1; 
        } 
        originalArray.sort(function(){ return 0.5 - Math.random(); }); 
        for (var i=0;i<count;i++){ 
        var luckyNum = originalArray[i]
        alert(luckyNum); 
        if(luckyNum === 1){
            window.location.href = "";
            return;
        }else if(luckyNum === 2){
            window.location.href = "";
            return;
        }else if(luckyNum === 3){
            window.location.href = "";
            return;
        }else if(luckyNum === 4){
            window.location.href = "";
            return;
        }
        }
    /**
     * Fourth option
     */
    var luckyNum = randUnique(1,4,1);
    alert(luckyNum); 
    if(luckyNum == 1){
        window.location.href = "";
    }else if(luckyNum == 2){
        window.location.href = "";
    }else if(luckyNum == 3){
        window.location.href = "";
    }else if(luckyNum == 4){
        window.location.href = "";
    }
    /**
    The fifth solution (it can solve the problem of entering the same page twice before and after)
    */
    var changeNum = Math.ceil(Math.random()*2);
    //alert("changeNum="+changeNum);
    if (changeNum == 1){
        var arr = [1,2];
        var skipNum = arr.splice(Math.floor(Math.random()*arr.length),1);
        //alert("skipNum="+skipNum);
        if(skipNum==1){
            window.location.href = "";
        }else if(skipNum==2){
            window.location.href = "";
        }
    } else if (changeNum == 2){
        var arr = [3,4];
        var skipNum = arr.splice(Math.floor(Math.random()*arr.length),1);
        //alert("skipNum="+skipNum);
         if(skipNum==3){
            window.location.href = "";
        }else if(skipNum==4){
            window.location.href = "";
        }
    }
    /*
    *Sixth solution (it can solve the problem of entering the same page twice before and after)
    */


    var firstArr = [ 1, 2 ];

    var secondArr = [ 3, 4 ];

    var skipNum = firstArr.splice(Math.floor(Math.random() * firstArr.length),
            1);
    // alert(skipNum);
    if (skipNum == globalNum) {
        skipNum = secondArr.splice(
                Math.floor(Math.random() * secondArr.length), 1);
    }

    globalNum = skipNum;

    window.sessionStorage.setItem("skipNum", skipNum);
    if (skipNum == 1) {
        window.location.href = "";
    } else if (skipNum == 2) {
        window.location.href = "";
    } else if (skipNum == 3) {
        window.location.href = "";
    } else if (skipNum == 4) {
        window.location.href = "";
    }

})




    /** 
     * Get non repeated random number 
     * @param integer start Random number minimum 
     * @param integer end Maximum random number 
     * @param integer size The minimum number of random number acquisition is 1, and the default is 1 
     * @return integer|array Such as 1 or [2,4,7,9] 
     */  
    function randUnique(start, end, size){  
        // All random values  
        var allNums = new Array;  

        // Judge and obtain the number of random numbers  
        size = size ? (size > end - start ? end - start : size) : 1;  

        // Generate random number interval array  
        for (var i = start, k = 0; i <= end; i++, k++) {  
        allNums[k] = i;  
        }  

        // Sort by array  
        allNums.sort(function(){ return 0.5 - Math.random(); });  

        // Gets the subscript range of the array from the first to the specified number  
        return allNums.slice(0, size);  
    } 

Posted by sford999 on Mon, 04 May 2020 09:14:24 -0700