A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
sample-rng-plot.py
Go to the documentation of this file.
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## @file
17# @ingroup core-examples
18# @ingroup randomvariable
19# Demonstrate use of ns-3 as a random number generator integrated with
20# plotting tools.
21#
22# This is adapted from Gustavo Carneiro's ns-3 tutorial
23
24
25import argparse
26import sys
27
28import matplotlib.pyplot as plt
29import numpy as np
30
31## Import ns-3
32try:
33 from ns import ns
34except 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
42def 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 ## Random number generator.
50 rng = ns.CreateObject("NormalRandomVariable")
51 rng.SetAttribute("Mean", ns.DoubleValue(100.0))
52 rng.SetAttribute("Variance", ns.DoubleValue(225.0))
53
54 ## Random number samples.
55 x = [rng.GetValue() for t in range(10000)]
56
57 # the histogram of the data
58
59 ## Make a probability density histogram
60 density = 1
61 ## Plot color
62 facecolor = "g"
63 ## Plot alpha value (transparency)
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
77if __name__ == "__main__":
78 main()