SHARE
TWEET

Untitled

a guest Aug 14th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var rand = function(min, max) {
  2.     return Math.random() * (max - min) + min;
  3. };
  4.  
  5. var getRandomItem = function(list, weight) {
  6.     var total_weight = weight.reduce(function (prev, cur, i, arr) {
  7.         return prev + cur;
  8.     });
  9.      
  10.     var random_num = rand(0, total_weight);
  11.     var weight_sum = 0;
  12.     //console.log(random_num)
  13.      
  14.     for (var i = 0; i < list.length; i++) {
  15.         weight_sum += weight[i];
  16.         weight_sum = +weight_sum.toFixed(2);
  17.          
  18.         if (random_num <= weight_sum) {
  19.             return list[i];
  20.         }
  21.     }
  22.      
  23.     // end of function
  24. };
  25.  
  26. var list = ['javascript', 'php', 'ruby', 'python','lost'];
  27. var weight = [0.15, 0.05, 0.3, 0.5, 0 ];
  28. let arrItem = []
  29. for (var i = 0 ; i < 100;i++) {
  30.   var random_item = getRandomItem(list, weight);
  31.  // console.log(random_item + '');
  32.   arrItem.push(random_item)
  33. }
  34. function searchExist(searchVal ,arr) {
  35.   var count = arr.filter(function(item) {
  36.     return item === searchVal
  37.   }).length;
  38.   console.log('searchVal:' +searchVal +'--:' count)
  39. }
  40.  
  41. for (var j = 0; k < list.length; j++) {
  42.   searchExist(list[j],arrItem)
  43. }
  44.  
  45. console.log('1')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top