ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
pm_distNorm::getNormRand Interface Reference

Generate and return a scalar or array of arbitrary rank of random values from the univariate Normal distribution with the specified input mean and optionally, with the specified input standard deviation std of the Normal distribution. More...

Detailed Description

Generate and return a scalar or array of arbitrary rank of random values from the univariate Normal distribution with the specified input mean and optionally, with the specified input standard deviation std of the Normal distribution.

The procedures of this generic interface are merely convenient wrappers around the procedures of setNormRand.

Parameters
[in]mean: The input scalar or array of the same shape as other array-like arguments, of the same type and kind as rand, representing the mean of the Normal distribution.
[in]std: The input scalar or array of the same shape as other array-like arguments, of the same type and kind as rand, representing the standard deviation of the Normal distribution.
(optional, default = 1., can be present only if mean is also present.)
Returns
rand : The output scalar or array of arbitrary rank, of
  1. type real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128),
containing the Normal-distributed random output value.


Possible calling interfaces

rand = getNormRand(mean, std = std)
rand = getNormRand(rng, mean, std = std)
Generate and return a scalar or array of arbitrary rank of random values from the univariate Normal d...
This module contains classes and procedures for computing various statistical quantities related to t...
Warning
The condition 0. < std must hold for the corresponding input arguments.
All warnings and notes for setNormRand also apply to the procedures of this generic interface.
These conditions are verified only if the library is built with the preprocessor macro CHECK_ENABLED=1.
Remarks
The procedures under discussion are impure.
The procedures under discussion are elemental.
On input, the procedures of this generic interface minimally require the argument mean to allow compile-time resolution of the procedure calls.
The procedures of this generic interface internally use the intrinsic Fortran RNG for uniform random number generation.
See also
getNormRand
setNormRand
setNormRandBox
getNormLogPDF
getNormCDF


Example usage

1program example
2
3 use pm_kind, only: SK
4 use pm_kind, only: IK, RK ! all real kinds are supported.
5 use pm_distNorm, only: getNormRand
8 use pm_io, only: display_type
9
10 implicit none
11
12 integer(IK), parameter :: NP = 1000_IK
13 real(RK), dimension(NP) :: mean, std, rand
14
15 type(display_type) :: disp
16 disp = display_type(file = "main.out.F90")
17
18 call setLinSpace(mean, x1 = -5._RK, x2 = +5._RK)
19 call setLogSpace(std, logx1 = log(0.1_RK), logx2 = log(10._RK))
20
21 call disp%skip()
22 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
23 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
24 call disp%show("! Generate random numbers from the Normal distribution.")
25 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
26 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
27 call disp%skip()
28
29 call disp%skip()
30 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
31 call disp%show("! Normal random number with a particular mean and default unity standard deviation.")
32 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
33 call disp%skip()
34
35 call disp%skip()
36 call disp%show("mean(1)")
37 call disp%show( mean(1) )
38 call disp%show("rand(1:3) = getNormRand(mean(1))")
39 rand(1:3) = getNormRand(mean(1))
40 call disp%show("rand(1:3)")
41 call disp%show( rand(1:3) )
42 call disp%skip()
43
44 call disp%skip()
45 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
46 call disp%show("! Normal random number with given mean and standard deviation.")
47 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
48 call disp%skip()
49
50 call disp%skip()
51 call disp%show("mean(1)")
52 call disp%show( mean(1) )
53 call disp%show("std(1)")
54 call disp%show( std(1) )
55 call disp%show("rand(1:2) = getNormRand(mean(1), std(1))")
56 rand(1:2) = getNormRand(mean(1), std(1))
57 call disp%show("rand(1)")
58 call disp%show( rand(1) )
59 call disp%skip()
60
61 call disp%skip()
62 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
63 call disp%show("! Normal random numbers with a fixed set of mean and standard deviation.")
64 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
65 call disp%skip()
66
67 call disp%skip()
68 call disp%show("mean(1:NP:NP/3)")
69 call disp%show( mean(1:NP:NP/3) )
70 call disp%show("std(1:NP:NP/3)")
71 call disp%show( std(1:NP:NP/3) )
72 call disp%show("rand(1:NP:NP/3) = getNormRand(mean(1), std(1))")
73 rand(1:NP:NP/3) = getNormRand(mean(1), std(1))
74 call disp%show("rand(1:NP:NP/3)")
75 call disp%show( rand(1:NP:NP/3) )
76 call disp%skip()
77
78 call disp%skip()
79 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
80 call disp%show("! Normal random number with a range of means and standard deviations.")
81 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
82 call disp%skip()
83
84 call disp%skip()
85 call disp%show("mean(1:NP:NP/3)")
86 call disp%show( mean(1:NP:NP/3) )
87 call disp%show("std(1:NP:NP/3)")
88 call disp%show( std(1:NP:NP/3) )
89 call disp%show("rand(1:NP:NP/3) = getNormRand(mean(1:NP:NP/3), std(1:NP:NP/3))")
90 rand(1:NP:NP/3) = getNormRand(mean(1:NP:NP/3), std(1:NP:NP/3))
91 call disp%show("rand(1:NP:NP/3)")
92 call disp%show( rand(1:NP:NP/3) )
93 call disp%skip()
94
95 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 ! Output an example rand array for visualization.
97 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98
99 block
100 integer(IK) :: fileUnit, i
101 integer(IK), parameter :: NP = 5000_IK
102 open(newunit = fileUnit, file = "getNormRand.RK.txt")
103 write(fileUnit,"(3(g0,:,' '))") ( getNormRand(+2._RK, std = 3.0_RK) &
104 , getNormRand(+0._RK, std = 1.0_RK) &
105 , getNormRand(-5._RK) &
106 , i = 1, NP &
107 )
108 close(fileUnit)
109 end block
110
111end program example
Return the linSpace output argument with size(linSpace) elements of evenly-spaced values over the int...
Return the logSpace output argument with size(logSpace) elements of logarithmically-evenly-spaced val...
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11726
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11508
This module contains procedures and generic interfaces for generating arrays with linear or logarithm...
This module contains classes and procedures for input/output (IO) or generic display operations on st...
Definition: pm_io.F90:252
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
Definition: pm_io.F90:11393
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
Definition: pm_kind.F90:268
integer, parameter RK
The default real kind in the ParaMonte library: real64 in Fortran, c_double in C-Fortran Interoperati...
Definition: pm_kind.F90:543
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
Definition: pm_kind.F90:540
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
Definition: pm_kind.F90:539
Generate and return an object of type display_type.
Definition: pm_io.F90:10282

Example Unix compile command via Intel ifort compiler
1#!/usr/bin/env sh
2rm main.exe
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example Windows Batch compile command via Intel ifort compiler
1del main.exe
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
4main.exe

Example Unix / MinGW compile command via GNU gfortran compiler
1#!/usr/bin/env sh
2rm main.exe
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example output
1
2!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4! Generate random numbers from the Normal distribution.
5!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7
8
9!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10! Normal random number with a particular mean and default unity standard deviation.
11!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12
13
14mean(1)
15-5.0000000000000000
16rand(1:3) = getNormRand(mean(1))
17rand(1:3)
18-5.3683824624367800, -5.3683824624367800, -5.3683824624367800
19
20
21!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
22! Normal random number with given mean and standard deviation.
23!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
24
25
26mean(1)
27-5.0000000000000000
28std(1)
29+0.10000000000000003
30rand(1:2) = getNormRand(mean(1), std(1))
31rand(1)
32-4.8176936429469901
33
34
35!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36! Normal random numbers with a fixed set of mean and standard deviation.
37!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38
39
40mean(1:NP:NP/3)
41-5.0000000000000000, -1.6666666666666665, +1.6666666666666670, +5.0000000000000000
42std(1:NP:NP/3)
43+0.10000000000000003, +0.46415888336127803, +2.1544346900318851, +10.000000000000004
44rand(1:NP:NP/3) = getNormRand(mean(1), std(1))
45rand(1:NP:NP/3)
46-4.9825134267578610, -4.9825134267578610, -4.9825134267578610, -4.9825134267578610
47
48
49!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50! Normal random number with a range of means and standard deviations.
51!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52
53
54mean(1:NP:NP/3)
55-5.0000000000000000, -1.6666666666666665, +1.6666666666666670, +5.0000000000000000
56std(1:NP:NP/3)
57+0.10000000000000003, +0.46415888336127803, +2.1544346900318851, +10.000000000000004
58rand(1:NP:NP/3) = getNormRand(mean(1:NP:NP/3), std(1:NP:NP/3))
59rand(1:NP:NP/3)
60-5.0890111408403875, -1.7740432547419096, +1.5570261651935300, -11.862232629955805
61
62

Postprocessing of the example output
1#!/usr/bin/env python
2
3import matplotlib.pyplot as plt
4import pandas as pd
5import numpy as np
6import glob
7import sys
8
9linewidth = 2
10fontsize = 17
11
12marker ={ "CK" : "-"
13 , "IK" : "."
14 , "RK" : "-"
15 }
16xlab = { "CK" : "Normal Random Number ( real/imaginary components )"
17 , "IK" : "Normal Random Number ( integer-valued )"
18 , "RK" : "Normal Random Number ( real-valued )"
19 }
20legends = [ r"$\mu = -5.,~\sigma = 1.0$"
21 , r"$\mu = 0.0,~\sigma = 1.0$"
22 , r"$\mu = 2.0,~\sigma = 3.0$"
23 ]
24
25for kind in ["IK", "CK", "RK"]:
26
27 pattern = "*." + kind + ".txt"
28 fileList = glob.glob(pattern)
29 if len(fileList) == 1:
30
31 df = pd.read_csv(fileList[0], delimiter = " ", header = None)
32
33 fig = plt.figure(figsize = 1.25 * np.array([6.4, 4.8]), dpi = 200)
34 ax = plt.subplot()
35
36 if kind == "CK":
37 plt.hist( df.values[:,0:3]
38 , histtype = "stepfilled"
39 , alpha = 0.5
40 , bins = 75
41 )
42 else:
43 plt.hist( df.values[:,0:3]
44 , histtype = "stepfilled"
45 , alpha = 0.5
46 , bins = 75
47 )
48 ax.legend ( legends
49 , fontsize = fontsize
50 )
51 plt.xticks(fontsize = fontsize - 2)
52 plt.yticks(fontsize = fontsize - 2)
53 ax.set_xlabel(xlab[kind], fontsize = 17)
54 ax.set_ylabel("Count", fontsize = 17)
55 ax.set_title("Histograms of {} Normal random numbers".format(len(df.values[:, 0])), fontsize = 17)
56
57 plt.grid(visible = True, which = "both", axis = "both", color = "0.85", linestyle = "-")
58 ax.tick_params(axis = "y", which = "minor")
59 ax.tick_params(axis = "x", which = "minor")
60
61 plt.savefig(fileList[0].replace(".txt",".png"))
62
63 elif len(fileList) > 1:
64
65 sys.exit("Ambiguous file list exists.")

Visualization of the example output
Test:
test_pm_distNorm


Final Remarks


If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.

  1. If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
  2. If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.

This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.

Author:
Amir Shahmoradi, Oct 16, 2009, 11:14 AM, Michigan

Definition at line 1849 of file pm_distNorm.F90.


The documentation for this interface was generated from the following file: