1 / 27

亂數函數 (Random-Number Function)

亂數函數 (Random-Number Function). Rung-Shiang Cheng Department of Computer and Communication, Kun Shan University rscheng@mail.ksu.edu.tw. Outline. 介紹亂數函數的使用方法 相關範例. 產生亂數 和 模擬生活中的隨機資料. 何時會用到亂數函數呢 ? 統計實驗 … 網路模擬 … ANSI C <stdlib.h>. 產生 10 個亂數,同時印出 RAND_MAX 的值 (1/2). Example. 把時間當成亂數的種子.

Download Presentation

亂數函數 (Random-Number Function)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 亂數函數(Random-Number Function) Rung-Shiang Cheng Department of Computer and Communication, Kun Shan University rscheng@mail.ksu.edu.tw

  2. Outline • 介紹亂數函數的使用方法 • 相關範例

  3. 產生亂數和模擬生活中的隨機資料 • 何時會用到亂數函數呢?統計實驗…網路模擬… • ANSI C <stdlib.h>

  4. 產生10個亂數,同時印出RAND_MAX的值(1/2) • Example 把時間當成亂數的種子

  5. 產生10個亂數,同時印出RAND_MAX的值(2/2) • 執行結果

  6. 取特定的值當作亂數種子(以"0"為例) • 第1次實驗…. 第2次實驗…. 第n次實驗

  7. 產生10個範圍從1到6的亂數 • Example: 丟骰子….

  8. 產生10個範圍從1到6的亂數 • 執行結果

  9. 產生0~1之間的浮點數亂數 • Example

  10. 產生0~1之間的浮點數亂數 • 執行結果

  11. 產生m到n之間的整數亂數 公式:m + (rand() % (n − m + 1) ) • Example:

  12. 產生m到n之間的整數亂數 • 執行結果

  13. 用指令列引數指定亂數的最小值m和最大值n

  14. 用指令列引數指定亂數的最小值m和最大值n • 執行結果

  15. 產生m到n之間的浮點數亂數 • 公式:m + (rand() / RAND_MAX) × (n − m)

  16. 產生m到n之間的浮點數亂數 • Case 1: random (20, 60)

  17. 產生m到n之間的浮點數亂數 • Case 2: random (0, 1)

  18. 利用NS-2網路模擬器的亂數產生器模擬封包遺失利用NS-2網路模擬器的亂數產生器模擬封包遺失 • NS-2是什麼? • Network Simulator, version 2 • 物件導向網路模擬器 • C++, OTCL • Router, Link, End point, TCP/IP protocols • Ethernet, WiFi, Sensor Networks

  19. 網路模擬的應用實例 • 使用網路模擬器模擬封包遺失 • 統計與量測模擬結果

  20. 如何在網路模擬器中使用亂數產生器 • 設定種子(seed) • 設定分佈 (distribution) • E.g. Pareto、Constant、Uniform、Exponential、HyperExponentail

  21. UniformDistribution • 參數 • 最小值: min_ • 最大值: max_ • 範例 • 執行結果 這個例子在NS-2中產生一個亂數產生器,並把種子的值設定為2(若種子的值為0的話,則亂數產生器會在每次程式被執行時使用不同的種子 )

  22. 模擬在網路上使用FTP傳送資料 • 網路拓撲 • 執行結果

  23. ErrorModel (Uniform Distribution)(1/2) • 修改TCL Script加入下列程式碼 在node0和node1之間( SimpleLink object)加入error • rate : Specify error rate • unit : Specify the unit of error (time, pkt, bits) • Default: packet • ranvar : Specify the random variable for generating errors

  24. ErrorModel (Uniform Distribution)(1/2) • 執行結果 和前一個例子相比,goodput下降了…

  25. 量測Error rate(1/2) • AWK程式碼(修改自sent_drop.awk) 計算送出的封包 計算收到的封包 在這裡略做修改…

  26. 量測Error rate(1/2) • 模擬&量測結果 模擬時間:5秒

  27. 如何找到這份講義? • 就這麼簡單 (Report) http://teachers.ksu.edu.tw/rscheng/illus/report.htm

More Related