當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


d3.js randomLogNormal()用法及代碼示例

d3.randomLogNormal()函數用於基於log-normal函數生成隨機數。

用法:

d3.randomLogNormal(mu,sigma);

參數:該函數接受上述和以下所述的兩個參數:

  • mu:它是隨機變量的期望值。
  • sigma:該數字以給定的標準偏差sigma生成。

返回值:它返回一個函數。

注意:如果未提供mu和sigma,則默認情況下mu等於0,sigma等於1。



以下給出了上述函數的一些示例。

範例1:每次執行該函數時,輸出可能會不同。

HTML

<!DOCTYPE html> 
<html lang="en"> 
<head> 
  <meta charset="UTF-8"> 
  <meta name="viewport" 
        content="width=device-width, initial-scale=1.0"> 
  <title>Document</title> 
</head> 
<style> 
</style> 
<body> 
  <!-- Fetching from CDN of D3.js -->
  <script type = "text/javascript" 
          src = "https://d3js.org/d3.v4.min.js"> 
  </script> 
  <script> 
    // Output may be different each time  
    // the function is run 
    console.log(d3.randomLogNormal(2,10)()) 
    console.log(d3.randomLogNormal(0,0)()) 
    console.log(d3.randomLogNormal(1,12)()) 
    console.log(d3.randomLogNormal(10,0)()) 
    console.log(d3.randomLogNormal(5,1)()) 
  </script> 
</body> 
</html>

輸出:

範例2:

HTML

<!DOCTYPE html> 
<html lang="en"> 
<head> 
  <meta charset="UTF-8"> 
  <meta name="viewport" 
        content="width=device-width, initial-scale=1.0"> 
  <title>Document</title> 
</head> 
<style> 
</style> 
<body> 
  <!-- Fetching from CDN of D3.js -->
  <script type = "text/javascript" 
          src = "https://d3js.org/d3.v4.min.js"> 
  </script> 
  <script> 
    // Output may be different each time  
    // the function is run 
    console.log(d3.randomLogNormal(2,10)(1)) 
    console.log(d3.randomLogNormal(0,0)(2)) 
    console.log(d3.randomLogNormal(1,12)(3)) 
    console.log(d3.randomLogNormal(10,0)(0)) 
    // When mu and sigma is not given 
    console.log(d3.randomLogNormal()()) 
  </script> 
</body> 
</html>

輸出:




相關用法


注:本文由純淨天空篩選整理自tarun007大神的英文原創作品 D3.js randomLogNormal() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。