A Discrete-Event Network Simulator
API
sample-rng-plot.py
Go to the documentation of this file.
1 #
2 # This program is free software; you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License version 2 as
4 # published by the Free Software Foundation
5 #
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU General Public License for more details.
10 #
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #
15 
16 
23 
24 
25 import argparse
26 import sys
27 
28 import matplotlib.pyplot as plt
29 import numpy as np
30 
31 
32 try:
33  from ns import ns
34 except ModuleNotFoundError:
35  raise SystemExit(
36  "Error: ns3 Python module not found;"
37  " Python bindings may not be enabled"
38  " or your PYTHONPATH might not be properly configured"
39  )
40 
41 
42 def main():
43  parser = argparse.ArgumentParser("sample-rng-plot")
44  parser.add_argument("--not-blocking", action="store_true", default=False)
45  args = parser.parse_args(sys.argv[1:])
46 
47  # mu, var = 100, 225
48 
49 
50  rng = ns.CreateObject("NormalRandomVariable")
51  rng.SetAttribute("Mean", ns.DoubleValue(100.0))
52  rng.SetAttribute("Variance", ns.DoubleValue(225.0))
53 
54 
55  x = [rng.GetValue() for t in range(10000)]
56 
57  # the histogram of the data
58 
59 
60  density = 1
61 
62  facecolor = "g"
63 
64  alpha = 0.75
65 
66  # We don't really need the plot results, we're just going to show it later.
67  # n, bins, patches = plt.hist(x, 50, density=1, facecolor='g', alpha=0.75)
68  n, bins, patches = plt.hist(x, 50, density=True, facecolor="g", alpha=0.75)
69 
70  plt.title("ns-3 histogram")
71  plt.text(60, 0.025, r"$\mu=100,\ \sigma=15$")
72  plt.axis([40, 160, 0, 0.03])
73  plt.grid(True)
74  plt.show(block=not args.not_blocking)
75 
76 
77 if __name__ == "__main__":
78  main()