Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Random Number?

I have the following script:

Timer=0;
function countdown(auctionid){
    var auctions;
    var divs;

    Timer=Timer+1;

    if((Timer%10=="0")||(Timer=="1")){
        $.get("current.php", {
                id:auctionid
            },
            function(data){
                auctions=data.split("||");
                for(n=0;n<=auctions.length;n++){
                    if(auctions[n] != undefined){
                        divis=auctions[n].split("##");

                        $('#futu'+divis[0]).html(divis[1]);
                    }
                }
            }
        );
    }

    var cauctionid="auctionid";
    var tauctions=auctionid.split("|");
    for(i=0;i<=tauctions.length;i++){
        if(tauctions[i] != undefined){
            var dd=$('#futu'+tauctions[i]).text();
            var cdd=dd-1;
            $('#futu'+tauctions[i]).html(cdd);

            dd=dd*1000;
            dday=Math.floor(dd/(60*60*1000*24)*1)
            dhour=Math.floor(dd/(60*60*1000)*1)
            dmin=Math.floor((dd%(60*60*1000))/(60*1000)*1)
            dsec=Math.floor(((dd%(60*60*1000))%(60*1000))/1000*1)

            if(dday==0&&dhour==0&&dmin==0&&dsec==0){
                $('#Bid'+tauctions[i]).html("SOLD");
            //return
            }
            if(dhour <=9){
                dhour = "0"+dhour;
            }
            if(dmin <=9){
                dmin = "0"+dmin;
            }
            if(dsec <=9){
                dsec = "0"+dsec;
            }

            if(dd>=1000){
                var valll=dhour+":"+dmin+":"+dsec;
            }

            if(dd<1000){
                var valll="00:00:00";
            }

            $('#Bid'+tauctions[i]).html(valll);
        }
    }
    refreshID=setTimeout("countdown('"+auctionid+"')",1000);
}

On the line that reads: if((Timer%10=="0")||(Timer=="1")){

How do i make the 10, a random number between 2 and 12?

like image 473
mrpatg Avatar asked Jul 14 '09 22:07

mrpatg


People also ask

How do you generate a random number in JavaScript?

Generating Javascript Random Numbers Javascript creates pseudo-random numbers with the function Math. random() . This function takes no parameters and creates a random decimal number between 0 and 1. The returned value may be 0, but it will never be 1.

How do you generate a random number between 1 and 3 in JavaScript?

In JavaScript, you can generate a random number with the Math. random() function.

Is JavaScript Math random really random?

How random is Math. random()? It may be pointed out that the number returned by Math. random() is a pseudo-random number as no computer can generate a truly random number, that exhibits randomness over all scales and over all sizes of data sets.


2 Answers

You want to use the random() function. There is no version that returns an integer unfortunately, only a float between 0 and 1, so you'll need to do a few operations. Try the following:

var randomNum = Math.floor(Math.random() * 10) + 2;

This should generate a random integral number between 2 (inclusive) and 12 (exclusive). Change 10 to 11 if you want the 12 to be inclusive, of course.

like image 132
Noldorin Avatar answered Oct 01 '22 17:10

Noldorin


See the answers to this question which also allows you to set the seed value.

like image 29
skamradt Avatar answered Oct 01 '22 19:10

skamradt