簡體   English   中英

從javascript中的pdf中選擇隨機變量

[英]Picking a random variable from a pdf in javascript

我遇到一些奇怪的錯誤,我無法理解它的來源。 我在Google Script環境中用js編寫。

function tester() {
  var pdf = [[0,5],[1,5],[2,40],[3,50]]; // some pdf as a 2d array
  var tuple = [0,0,0,0]; //the resulting pdf from the test
  var rand = 0;

  for (var i = 0; i<100; i++){ //100 times initialize a random variable and then catch the result into the tuple 
    rand = getRandomN(pdf);
    if (rand==0){tuple[0]+=1} //if the outcome==0 then add 1 to the first element of the tuple
       else if (rand==1){tuple[1]+=1}
       else if (rand==2){tuple[2]+=1}
       else if (rand==3){tuple[3]+=1}
  }

  Logger.log(tuple);
}

getRandomN(pdf)根據getRandomN(pdf)返回一個結果

問題在於,在某些位置,元組總是返回全零且帶有1。 看起來隨機化器工作得很好,但是循環只經過了一次。 有人暗示嗎?

更新:

function getRandomN(pdf) {
  var result = 0;
  var rand = getRandomInt(0,10000)/100;

  for (var i=1; i<pdf.length; i++){
    pdf[i][1] = pdf[i][1] + pdf[i-1][1];

    }

  if (pdf[pdf.length-1][1] != 100){return undefined}

  //Logger.log(rand);
  for (var i=0; i<pdf.length; i++){
    if (rand<=pdf[i][1]){result=pdf[i][0]; break}

  }
  Logger.log(pdf);
return result;
}

Mozilla的標准功能

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}

原因是:

  if (pdf[pdf.length-1][1] != 100){return undefined;}

在這里,如果return 0 or any of rand first index則返回undefined,它將顯示正確的tuple並且您可以看到循環計數。

嘗試運行此:

  function tester() { var pdf = [[0,5],[1,5],[2,40],[3,50]]; // some pdf as a 2d array var tuple = [0,0,0,0]; //the resulting pdf from the test var rand = 0; for (var i = 0; i<100; i++){ //100 times initialize a random variable and then catch the result into the tuple rand = getRandomN(pdf); tuple[rand] += 1; } console.log(tuple); document.write(tuple); } function getRandomN(pdf) { var result = 0; var rand = getRandomInt(0,10000)/100; // console.log(rand); for (var i=1; i<pdf.length; i++){ pdf[i][1] = pdf[i][1] + pdf[i-1][1]; } if (pdf[pdf.length-1][1] != 100){return 0;}//return any of 0,1,2,3 to test your code. for (var i=0; i<pdf.length; i++){ if (rand<=pdf[i][1]){result=pdf[i][0]; break} } // console.log(pdf); return result; } function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; } tester(); 

我想我知道為什么。 由於pdf的范圍在tester()內是完全全局的,並且我正在getRandomN(pdf)內進行更改,因此,它的范圍一直在增加,並且在第一次運行后便得到更新,並且我已經在從新計算pdf,其中pdf的最后一個元素(即cdf)永遠不會等於100。更新:即使您對正在運行的正確代碼感興趣。 將pdf映射到cdf的部分並不是最漂亮的部分。 我希望能有改進的提示,但效果很好。 感謝貢獻者指出正確的方向。

function getRandomN(pdf) {
  var result = 0;
  var rand = getRandomInt(0,10000)/100;
  var cdf = [];

  //construct the cdf
  for (var i=1; i<pdf.length; i++){
    //handle the first unchanged element
    cdf[0]=[];
    cdf[0][1] = pdf[0][1];
    cdf[0][0] = pdf[0][0];

    cdf[i]=[];
    cdf[i][1] = pdf[i][1] + cdf[i-1][1];
    cdf[i][0] = pdf[i][0];//add all outcomes to the array's first column
    }

  if (cdf[cdf.length-1][1] != 100){return undefined}

  //Logger.log(rand);
  for (var i=0; i<cdf.length; i++){
    if (rand<=cdf[i][1]){result=cdf[i][0]; break}
    }

  //Logger.log(cdf);
return result;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM