'Random number generator is giving zero still when minimum and maximum is given

var prevRandomNum = -1;
var arr=[];
var math=[];

window.onload=function(){
    var ref = firebase.database().ref("manga");
ref.once("value", function(snapshot) {
  
    snapshot.forEach(function(childSnapshot) {
    var childKey = childSnapshot.key;
    arr.push(childKey);

});

  }); 
}
function g(){
     const btn = document.getElementById("btng");
    btn.innerText = "Suggest me another";
    var sorry="im/sorry.png";
console.log(arr.length);
    var randomNum;
   var calc=arr.length-1;
if(math.length==calc){
            document.getElementById("mname").textContent= "Sorry you have reached the end of the manga list i have suggested.";
         document.getElementById("mgenre").textContent= "I will be uploading new suggestions soon.";
         document.getElementById("mchap").textContent= "";
         document.getElementById("mstatus").textContent= "";
            document.getElementById("img").src=sorry;
         document.getElementById("pic").style.backgroundImage=`url(${sorry})`;
        
        }
        else{
           do{ 
        randomNum= Math.floor(Math.random() * (arr.length-1) )+1;
        
    
       console.log("rand "+ randomNum);
        
        
        }while(randomNum=== prevRandomNum||math.includes(randomNum));
        
    
    prevRandomNum = randomNum;
math.push(prevRandomNum);
} 
       

Above is the code I am using to retrieve data from firebase database and get value of a specific parent node using random number, i have stored push key in array and i am using random number generator to get a unique and random parent data every time when the button is pressed but the random is giving out 0 as random number even while minimum and maximum number is given. You can check live preview here https://kazemanga.xyz/ please open the console to see if the random number is zero or not. Any help would be appreciated.

arr.length=7 (the no of parent node on db is 7) It may not give random no as Zero on first click so plz refresh and try again, it doesn't give zero sometimes and when I refresh page and try again it gives me.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source