Javascript Get Random result with probability for specific array

这一生的挚爱 提交于 2020-01-05 07:07:20

问题


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.png","12.65"],
                      ["images/fantastic-word.png","10.00"],
                      ["images/free-product.png","15.50"]
                      ];

        var pos = Math.floor((Math.random() * shirts.length) + 0);
        $("#image").html($("<img/>").attr("src", shirts[pos][0]));
        $(".price").html("$" + shirts[pos][1]);

i have do the basic math.random() to make it random show the image, now i need to make it show with probability, for example probability for showing ["images/fantastic-logo.png","12.65"] was 50%, ["images/fantastic-word.png","10.00"] was 25%, ["images/free-product.png","15.50"] was 25%.

Thanks for everybody help


回答1:


One option is add a third element which indicate the weight of probability.

In the example below fantastic-logo.png has 2 to represent 50% and the other 2 only as 1 to represent 25% each.

Then create a 4 element array [0,0,1,2] - This represent element 0 has 50% chance. element 1 has 25% chance and element 2 has 25% as well.

Make random from the newly created array and use the value as the position.

Like:

var shirts = [
  ["images/fantastic-logo.png", "12.65", 2],
  ["images/fantastic-word.png", "10.00", 1],
  ["images/free-product.png", "15.50", 1]
];

//Create a 4 element array based on probability weight
var probability = shirts.map((v, i) => Array(v[2]).fill(i)).reduce((c, v) => c.concat(v), []);

//Random select from probability array
var pos = probability[Math.floor((Math.random() * probability.length))];

$("#image").html($("<img/>").attr("src", shirts[pos][0]));
$(".price").html("$" + shirts[pos][1]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="image"></div>
<div class="price"></div>



回答2:


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




回答3:


For a short array this should be enough, using getRandomInt from Mozilla Developers:

function getRandomInt(max) {
    return Math.floor(Math.random() * Math.floor(max));
}

var shirts = [
    ["images/fantastic-logo.png", "12.65"],
    ["images/fantastic-word.png", "10.00"],
    ["images/free-product.png", "15.50"]
];

var random = getRandomInt(100);
var selectedShirt;
if (random <= 50) {
    selectedShirt = shirts[0];
} else if (random > 50 && random < 75) {
    selectedShirt = shirts[1];
} else {
    selectedShirt = shirts[2];
}

$("#image").html($("<img/>").attr("src", shirts[selectedShirt][0]));
$(".price").html("$" + shirts[selectedShirt][1]);

Note that you can use less numbers like in Ray's answer. For a bigger array you may use a better approach.



来源:https://stackoverflow.com/questions/49164635/javascript-get-random-result-with-probability-for-specific-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!