A Discrete-Event Network Simulator Home Tutorials  ▼ Docs    ▼ Develop ▼ API
sample-rng-plot.py
Go to the documentation of this file.
1 # -*- Mode:Python; -*-
2 # /*
3 # * This program is free software; you can redistribute it and/or modify
4 # * it under the terms of the GNU General Public License version 2 as
6 # *
7 # * This program is distributed in the hope that it will be useful,
8 # * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # * GNU General Public License for more details.
11 # *
12 # * You should have received a copy of the GNU General Public License
13 # * along with this program; if not, write to the Free Software
14 # * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 # */
16
17 ## @file
18 # @ingroup core-examples
19 # @ingroup randomvariable
20 # Demonstrate use of ns-3 as a random number generator integrated with
21 # plotting tools.
22 #
23 # This is adapted from Gustavo Carneiro's ns-3 tutorial
24
25
26 import numpy as np
27 import matplotlib.pyplot as plt
28 import ns.core
29
30 # mu, var = 100, 225
31 rng = ns.core.NormalRandomVariable()
32 rng.SetAttribute("Mean", ns.core.DoubleValue(100.0))
33 rng.SetAttribute("Variance", ns.core.DoubleValue(225.0))
34 x = [rng.GetValue() for t in range(10000)]
35
36 # the histogram of the data
37 n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)
38
39 plt.title('ns-3 histogram')
40 plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
41 plt.axis([40, 160, 0, 0.03])
42 plt.grid(True)
43 plt.show()