Javascript Get Random result with probability for specific array

前端 未结 3 982
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 07:15

i have a array and i need to do it randomly show the output by probability below are my code

var shirts = [
                      [\"images/fantastic-logo.pn         


        
3条回答
  •  被撕碎了的回忆
    2020-12-20 07:27

    function getRandomInt(max) {
      return Math.floor(Math.random() * Math.floor(max));
    }
    /* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random */
    
    getRandomInt(3)

    If you get 0 or 1, the first image is selected. But this method is not elegant. Please refer to the link below.

    Generate A Weighted Random Number

提交回复
热议问题