Javascript Random Number?

前端 未结 3 1705
傲寒
傲寒 2020-12-16 14:14

I have the following script:

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

    Timer=Timer+1;

    if((Timer%10==\"0\")||(Timer==\         


        
相关标签:
3条回答
  • 2020-12-16 14:16

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

    0 讨论(0)
  • 2020-12-16 14:26

    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.

    0 讨论(0)
  • 2020-12-16 14:29

    Using the Web Crypto api.

    console.log(
    
      crypto.getRandomValues(new Uint32Array(1))[0] ,
    
      crypto.getRandomValues(new Uint16Array(1))[0] ,
      
      crypto.getRandomValues(new Uint8Array(1))[0]
      
    )

    https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues https://caniuse.com/#feat=cryptography

    0 讨论(0)
提交回复
热议问题