Integration - Page 1
(D36)
INFORMATIK I November 2007 (WFvG/Zrinka)
(dk:word2000 version)
5. Numerical Integration of Functions, using Examples from Thermodynamics
Contents Page
5.1 Introduction 2
5.2 Integration using the polynomial approximation 3
5.3 Monte Carlo integration 5
5.4 Romberg’s integration method 6
5.5 Other integration methods 9
5.6 Thermodynamic state functions 10
5.7 Exercises 13
A. Program to compute the entropy of solid triptycene
using the trapezoid formula and Simpson’s rule 14
B. Program to compute the Helmholtz free energy of gaseous
carbon dioxide using Monte Carlo integration 15
C. Program to compute the heat capacity of solid copper as
a function of temperature using Romberg integration 16
5.8 References 18
5.9 Appendix: function rando 19
Integration - Page 2
5.1 Introduction and Overview
In this exercise the following type of problem is considered. Given a function f(x), which
represents the rate of change of some quantity with respect to the variable x, find the function
F(x) describing the variation of the quantity with x. In other words, find F(x) such that its
derivative is f(x):
dF(x) F (x) = f(x)
dx (5.1)
The inverse operation of differentiation is called integration:
F(x) = f(x) dx + constant (5.2)
Since the derivative of a constant function is zero, the so-called primitive function F(x) of f(x) is
not completely defined: an arbitrary constant can be added to F(x) without violating (5.1).
Therefore, (5.2) is called an indefinite integral. If integration limits are given, the integral
becomes a definite integral: b
a
I = f(x) dx = F(b)- F(a) (5.3)
In (5.3), f(x) is called the integrand.
In this exercise different methods to perform numerical integration of a function are
discussed. They are applied to compute different thermodynamic quantities for gases and solids.
In Exercise A the entropy of solid triptycene is calculated by integration of a function related to
the heat capacity measured as a function of temperature. The trapezoidal approximation and
Simpson’s rule are compared. Exercise B deals with the calculation of the change in free
energy upon change of volume of a (non-ideal) van der Waals gas. Here, the technique of
Monte Carlo integration is applied. Exercise C deals with the variation of the heat capacity of
a monatomic solid with temperature. The Debye formula is integrated using the Romberg
integration formula.
Integration - Page 3
5.2 Integration using the polynomial approximation
The integral
b
a
I = f(x) dx (5.4)
can geometrically be interpreted as the area enclosed by the curve y=f(x), the lines x=a and x=b,
and the x-axis (figure 1).
Figure 1
The integral can be approximated by dividing the interval (a,b) into N subintervals of equal
length
b ah =
N (5.5)
and by approximating f(x) for each subinterval using a polynomial of degree m.
The simplest polynomial approximation is the rectangular approximation, in which the value
of f(x) in each subinterval (xn, xn+h) is approximated by f(xn). Here we use
0nx = x + nh n = 0,1,2,...N (5.6)
with x0 = a and xN = b. The rectangular approximation IRA(N) of I is
Integration - Page 4
0 1 1RA N
N 1
n
n=0
I (N) = [f(x ) + f(x )+...+ f(x )] h
= h f(x ). (5.7)
The deviation of IRA(N) from I is proportional to N-1, or to h, if expressed in terms of the
subinterval length (see Gould and Tobochnik, 1988).
A better approximation is obtained by approximating f(x) for each subinterval (xn,xn+1) by a
straight line connecting f(xn) and f(xn+1). The area under this line segment equals [f(xn) + f(xn+1)] h
/ 2. Summation of the areas of all subintervals yields the trapezoidal approximation IT of I:
0 1 1
0
T N N
N 1
n N
n=1
I (N) = [f(x ) + 2f(x ) + ... + 2f(x ) + f(x )] h/2
= [f(x ) / 2 + f(x ) + f(x ) / 2 ] h. (5.8)
The deviation of IT(N) from I is proportional to N-2, or expressed in terms of h, proportional to h2
(see Gould and Tobochnik, 1988).
An even better approximation can be obtained by approximating f(x) for each pair of
adjacent subintervals (xn, xn+1) and (xn+1,, xn+2) by a parabola p(x) through the points f(xn), f(xn+1),
and f(xn+2):
1 2
2
1
.
.
.
n nn
n n+1 n n+2
n nn+1
n+1 n n+1 n+2
n nn+2
n+2 n n+2 n+1
(x x ) (x x )p(x) = f( ) +x
( ) ( )x x x x
(x x ) (x x ) f( ) +x
( ) ( )x x x x
(x x ) (x x ) f( ) x
( ) ( )x x x x
+ +
+
+
(5.9)
Indeed, using the x-values xn, xn+1 and xn+2 in (5.9) we find p(xn ) = f(xn), p(xn+1) = f(xn+1) and
p(xn+2) = f(xn+2). The area under the curve p(x) between xn and xn+2 can be found by
straightforward integration of (5.9):
n+2
n
x
n n+1 n+2
x
p(x)dx = [f( ) + 4f( ) + f( )] h/3 .x x x (5.10)
Summation of the areas of all pairs of subintervals yields Simpson’s rule IS of I:
Integration - Page 5
( ) ( ) ( ) ( ) ( )
( ) ( )
( ) ( ) ( ) ( )
0 1 2 3 4
1
0
S
N N
N 1 N 2
n n Nn=1 n=2
n=odd n=even
I (N) = [f x + 4f x + 2f x + 4f x + 2f x +
... + 4f x + f x ] h/3
= [f x + 4 f x + 2 f x + f x ] h/3
(5.11)
where N must be even. The deviation of IS(N) from I is proportional to N-4, or expressed in terms
of h, proportional to h4 (see Gould and Tobochnik, 1988).
5.3 Monte Carlo integration
A simple numerical integration technique is the Monte Carlo method, which is also based on the
geometric interpretation of
b
a
I = f(x) dx (5.12)
as the area enclosed by the curve y = f(x), the lines x = a and x = b, and the x-axis (Fig. 1). We
construct a rectangle in the (x, y) plane, which has its vertical sides along the lines x=a and x=b,
and its horizontal sides along the lines y = 0 and y = H, where
maxH f(x) on [a,b] (5.13)
The area of this rectangle is H(b-a), see Fig. 2 (overleaf).
In an N-point Monte Carlo approximation IMC(N) of the integral I, N pairs of real numbers
(xn, yn) are sampled from a uniform rectangular distribution:
n
n
a bx n = 1, 2, ..., N
0 Hy (5.14)
Integration - Page 6
Figure 2
The fraction of points (xn, yn) in the rectangle that satisfy the condition
nn f ( )y x (5.15)
approximates the ratio of the integral I to the area of the rectangle. So, the N-point Monte
Carlo approximation IMC(N) of I becomes
N1
MC n
n=1
(N) = H (b a) NI (5.16)
with
nn
n
nn
1 if f( )y x =
0 if > f( )y x (5.17)
This approximation of I is not very accurate. For large N, the error is proportional to N- (see
e.g. Gould and Tobochnik, 1988). The Monte Carlo technique is especially suited for multi-
dimensional integrals.
5.4 Romberg’s integration method
An integration method which is considerably more accurate than the ones discussed so far, is
Integration - Page 7
Romberg’s method. It is based on the trapezoid formula (5.8), which is successively applied
using an increasing number of intervals N. At each iteration the length h of the subinterval in
(5.8) is halved, that is, the number of points N is doubled. The Romberg approximation IR(N)
of the integral I is then found by linear combination with results from the previous iteration
such that the truncation error is minimized. Continued iteration allows to achieve very high
accuracy.
Using the lowest possible value of N=1, the subinterval length (5.5) becomes
0h = b a (5.18)
which yields the lowest (zero-th) order trapezoid approximation (5.8):
0 0 TT (h ) I (N = 1)
0
= [f(a) + f(b)] h /2 (5.19)
2 4
2 0 4 0= I +c h + c h + K
The last line expresses the fact that the error of the trapezoid formula is of order h2, and
contains only terms of even order in h (see Gould and Tobochnik, 1988). The symbols c2 and c4
denote constants. Formula (5.19) defines the zero-th order Romberg approximation of the
integral I:
0 0RI (N = 1) T (h ) (5.20)
The next step is to apply the trapezoid formula (5.8) using a subinterval length that is
halved
1 0h = h /2 (5.21)
We find
0 T1( ) = (N = 2)hT I
1 1
= [f(a) + 2f(a+h ) + f(b)] h /2 (5.22)
2 4
2 1 4 1= I + c h + c h + K
where the last line in (5.22) is obtained by substitution of h1 for h0 in (5.19). The first-order
Romberg approximation is obtained by taking the linear combination of T0(h0) and T0(h1) for
which the terms with coefficient c2 in (5.19) and (5.22) cancel:
Integration - Page 8
1 1 0 1 0 0
4
4 1
T (h ) [4T (h ) T (h )] / 3
4 16 = I + c h + ...
3
(5.23)
The error of this first-order Romberg approximation
( ) ( )1 12
RI N T h = (5.24)
is proportional to h14, it is smaller by two orders of magnitude compared to the errors of either
(5.20) or (5.22).
The basic idea of Romberg integration is to repeat this procedure over and over. So, the
subinterval is halved again
2
2 1 0/ 2 / 2 ,h = h = h (5.25)
the trapezoid formula gives
0 2 TT (h ) = I (N = 4)
2
2 2 22= f(a) + 2f(a+h ) + + 2f(a+( 1)h ) + f(b) h / 2K (5.26)
2 4
2 2 4 2= I + c h + c h + K
where the last line in (5.26) is obtained by substitution of h2 for h0 in (5.19). The second order
term is eliminated as before
1 2 0 2 0 1
4
4 2
T (h ) = [4T (h ) T (h )] / 3
4 16 = I + c h +
3K
(5.27)
The term proportional to h24 can be eliminated by taking the linear combination
2 2 1 2 1 1
6
2
T (h ) [16T (h ) T (h )] / 15
= I + O(h ) (5.28)
where O(h26) denotes terms that are proportional to h2
6 or higher powers of h2. The error of this
second-order Romberg approximation
2 2RI (N = 4) T (h ) (5.29)
is proportional to h26, so (5.29) is again two orders of magnitude more accurate than (5.24).
Integration - Page 9
For the m-th Romberg iteration we find
1 0
m
m mh = h / 2 = h / 2 , (5.30)
0 m mT (h ) = [ f(a) + 2f(a+h ) + K
mm m2f(a + ( 1) h ) + f(b)] h / 22 (5.31)
2
m= I + O(h )
and
1 0 0 1
4
m m m
m
T (h ) = [4T (h ) T (h )] / 3
= I + O(h ) (5.32)
and so on for T2(hm), T3(hm), or generally
1 1 1
2 2
4 4n n
n m n m n m
n
m
T (h ) = [ T (h ) T (h )] / ( 1)
= I + O(h )+
(5.33)
The error of the m-th order Romberg approximation
2m
R m mI (N = ) T (h ) (5.34)
is proportional to hm2m+2.
The complete procedure can be schematically displayed as follows:
T0(h0)
T0(h0/2) T1(h0/2)
(5.35)
T0(h0/4) T1(h0/4) T2(h0/4)
T0(h0/8) T1(h0/8) T2(h0/8) T3(h0/8)
… … … … …
… … … … ... Tm-1(h0/2m-1)
T0(h0/2m) T1(h0/2
m) T2(h0/2m) T3(h0/2
m) ... Tm-1(h0/2m) Tm(h0/2
m)
Integration - Page 10
The lines of this diagram are sequentially processed from left to right. The quantities in the k-th
column are of order h2k accuracy. The best approximation is found in the right-hand lower
corner. The Romberg iterations can be stopped when IR(N=2m) = Tm (h0 /2 m) differs little from
the previous Romberg approximation IR(N=2m-1) = Tm-1(h0/2m-1). For a more extensive
discussion of Rombergs formulae, we refer to Fröberg (1966).
5.5 Other integration methods
There exist other very accurate integration methods, such as the different Gauss integration
methods, which will not be discussed here. For variations on the methods discussed in the
previous sections we refer to Press et al. (1987) who also compare the relative merits of the
various techniques.
Integration - Page 11
5.6 Thermodynamic state functions
When performing experiments, we carry out measurements on a more or less complex system,
which may contain gases, liquids and solids. A system is a part of the world separated from the
rest of the world by definite boundaries. The world outside the system is called its
surroundings. The system is called isolated if the boundaries do not permit any change of the
surroundings to affect the system and vice versa. The measured properties of a system allow
for a description of it. The state of a system is defined by a complete description of it. A
quantity or property of a system is called a state function when its value depends only on the
state of the system, not on the history of the system before it reached this state.
Thermodynamics concerns itself with state functions and the relations between them. In
Exercise 2 an example of such a relation was given. For a given amount of a pure gas, the three
thermodynamic quantities volume V, pressure P and temperature T are related by an equation
of state (see e.g. Moore, 1972). At low densities the ideal gas equation of state is valid
PV = nRT (5.36)
where n is the number of moles of gas and R is the universal gas constant. At higher densities
the van der Waals equation of state
( )2
2
nP + a V nb = nRT
V
(5.37)
yields a more appropriate description of the properties of the gas. Here, two additional
parameters, a and b, are introduced, which depend on the type of gas. The parameter a
introduces a correction based on the non-zero interaction between molecules when they come
closer to each other at higher gas densities. The parameter b introduces a correction based on
the finite volume of a molecule.
The relation between thermodynamic quantities is sometimes concisely expressed using
partial derivatives. For example,
T
A = P ,
V (5.38)
Integration - Page 12
the partial derivative of the so-called Helmholtz free energy A with respect to the volume V at
constant temperature T is equal to the negative value of the pressure P of the system. This
means that if we measure the change in Helmholtz free energy upon changing the volume,
without changing the temperature of the system, we will find a value equal to minus the
pressure of the system. Relation (5.38) can also be cast in the form of an integral:
2 1
2
1
V
V
A A = P dV (constant T) (5.39)
The change in free energy of the system is expressed as the work done by changing the volume
against the pressure. Using the equation of state for the ideal gas (5.36) the integration can be
performed analytically:
2 1ln 2 1A A = nRT ( / )V V (5.40)
For a van der Waals gas, expression (5.39) becomes
2 1
2
1
V 2
2
V
nRTanA A = - dV
V - nbV
(5.41)
This integral can be evaluated analytically:
( )2
2 1ln
1-1 -1
1 2
2
- nbVA A = an - + nRT V V
- nbV
(5.42)
Another thermodynamic state function is the entropy S of a system, which can be defined
as follows. A small change in entropy S is the heat Q absorbed by a system during a
reversible process at constant temperature T, divided by the temperature T:
S Q / T (5.43)
The heat capacity Cp of a system at constant pressure is defined as
p
Q C
T (5.44)
where Q is the amount of heat that is required to change the temperature of the system by T.
Integration - Page 13
Generally, the heat capacity of a substance depends on the temperature: Cp(T). For a reversible
process we find by combining (5.43) and (5.44)
1
pS = C (T) T T (5.45)
If we integrate this expression between two temperatures we get
1
2 1
2
1
T
p
T
S S = C (T) T dT (5.46)
If the heat capacity Cp(T) is given numerically, the integration can be carried out by numerical
techniques.
The heat capacity of a solid is a complex function of temperature. According to the
Einstein-Debye model the heat capacity at constant volume is
( )( )
43
2
0
9
1
DT T x
V Dx
e xC = R T T dx
e
(5.47)
where TD is the so-called Debye temperature, which has a characteristic value for each solid
(see e.g. Moore, 1972). Given TD , the heat capacity can be obtained for any temperature T by
numerical integration of (5.47).
Integration - Page 14
5.7 Exercises
The assignment must be completed according to the scheme. You must create a file which is a
fully functional program. The outputs of the runs with the created program must be included in
this file as a comment following the body of the program. Your report (maximum 1 page) must
be included as a comment at the head of the file, preceded by your name, login name, date and
option (A, B, or C) you chose. So, the file outline should be:
// program integrate
//
// student name Hans Busch
// login name hbusch
// date Nov. 10, 2007.
// option A
// Report: ........................
// ........................
// A program to calculate ..................
[ here follows the program ]
// outputs of executed program for various
// input parameter settings
Once you have made this file, you have to do the following:
Make a printout of yourfile.cc and put it in the appropriate mailbox in HCI G 238 and
e-mail the file using to [email protected].
yourfile.cc is the name you gave to your C++ file.
If you have used the input.files, please e-mail them also so that the assistants know what
you have used for the input parameters.
Please check the email address!!! Exercises sent to the wrong email will not be corrected.
You have the choice of the following exercises, in order of increasing difficulty.
Integration - Page 15
A. Program to compute the entropy of solid triptycene using the trapezoid formula and
Simpson’s rule
Andrews and Westrum (1970) have measured the heat capacity Cp of solid triptycene, that
is, 9,10-benzeno-9, 10-dihydroanthracene (C20H14) at a range of temperatures.
T
Cp
T
Cp
T
Cp
(K)
(JK-1mol-1)
(K)
(JK-1mol-1)
(K)
(JK-1mol-1)
20
18.00
120
111.6
220
200.9
40
44.56
140
127.8
240
221.0
60
64.43
160
144.9
260
241.8
80
80.88
180
162.8
280
263.1
100
96.15
200
181.5
300
284.7
From http://www.igc.phys.ch/education/Basic/exe/num_int/ you can download the data
file (triptycene.dat). Write a C++ program that computes the integral (5.46) numerically, both
employing the trapezoid formula (5.8) and Simpson’s rule (5.11). The program should have
two main functions that evaluate the integral by the trapezoid formula and by Simpson’s rule,
respectively. For the function that calculates the trapezoid formula, use a variable upper
integration limit (pass it as an argument). Make the program read a series of N + 1 equidistant
T-values and the corresponding Cp-values from file. Use it to calculate the entropy difference
of triptycene between T1 = 20 K and T2 = 300 K. Use the value N = 14 for both methods.
Compare the results.
When calculating the absolute entropy (that means T1 = 0 K) at T2, the integral (5.46) has a
singularity at T = 0 K. Avoid the singularity at T = 0 K by using the experimental observation
that Cp is proportional to T3 at low temperatures and Cp = 0 at T = 0 K. Using the given data we
find e.g.
Compute and plot the absolute entropy of triptycene as a function of T, using the trapezoid
formula.
3
p = 18.00 (T / 20 0 T 20K)C
Integration - Page 16
B. Program to compute the Helmholtz free energy of gaseous carbon dioxide using
Monte Carlo integration
The van der Waals constants for gaseous carbon dioxide (CO2) are a = 0.364 Nm4 mol-2
and b = 0.427 10-4 m3 mol-1 (Moore, 1972). The gas constant is R = 8.31441 JK-1 mol-1.
Write a C++ program that computes the integral (5.41) numerically employing the Monte
Carlo technique of section 5.3, formula (5.16). The given function rando can be used to
generate random numbers that are uniformly distributed on the interval (0, 1). See the appendix
how to use this function. Make the program read the following data:
N = number of Monte Carlo sample points
V1 = lower limit of the integration interval
V2 = upper limit of the integration interval
a = van der Waals parameter a of the substance
b = van der Waals parameter b of the substance
n = number of moles of the substance
T = temperature
R = gas constant
H = parameter of the integration procedure [see eq. (5.13)].
The program can be tested by setting a = b = 0 and comparing the result for very large N
to the exact value obtained from (5.40). Use the program to calculate the change in Helmholtz
free energy A of one mole of carbon dioxide when it is compressed at T = 313.15 K from a
volume V1 = 0.0257 m3 to a volume V 2 = 0.00247 m3. Compute A using 3 different sequences
of random numbers and use N = 1000, N = 10000 and N = 100000. Use also two different H-
values, which are at least a factor 10 different. Make a table containing the 18 A values and
the exact value of A calculated using (5.42), and the value of A obtained using the ideal gas
approximation (5.40). Analyze the differences and discuss the accuracy and convergence of
the Monte Carlo simulations with respect to H and N.
Integration - Page 17
C. Program to compute the heat capacity of solid copper as a function of temperature
using Romberg integration
Write a C++ program that computes the integral (5.47) numerically using the Romberg
integration scheme, discussed in section 5.4. Make the program read the following data:
: the Romberg iterations are terminated if two successive approximations of the integral
I differ less than
TD: Debye temperature of the substance
T : temperature for which CV is to be calculated
R : gas constant
Take 10-3 as a lower limit of the integral in (5.47) instead of the value 0.0 in order to avoid
numerical problems. For T/TD = 0.3 you should obtain CV 15 JK-1 mol-1. The Debye
temperature for Cu is TD = 315 K. Use the program to calculate the heat capacity CV for Cu at
the following temperatures: T = 10, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360 K.
Plot CV as a function of T. Use the program to determine the dependence of CV on T (i) as
T , and (ii) as T 0. Use R = 8.31441 J K-1 mol-1 and = 10-3.
The following sketch of a program may serve as a guide when writing the program based
on scheme (5.35):
1. comments about purpose, contents, author, date, etc.
2. definition of variables as needed
3. use a two-dimensional array TR [maxN] [maxN] to store the values of Tn(hm) in TR[n, m];
use maxN=20.
4. function to compute f(x) = ex x
4 (e
x-1)
-2 , see (5.47)
5. outline of the core of the program:
- print text
- read values of , TD, T and R, print these
- check for not-allowed input values
- set a = 0.001 and b = TD/T
- initialise the order m of the approximation, the size h0 of the subinterval and the value
of T0 (h0):
m = 0;
Integration - Page 18
h = b-a;
TR[0][0] = ( f(a) + f(b) ) * h / 2;
- make a double loop in which scheme (5.35) is processed line by line (control variable m)
and within each line from left to right (control variable n):
do
Increment m { next line in (5.35) }
Divide h by 2
Calculate TR[0][m] following eq. (5.31):
a) sum up odd (j=1,3,..) terms 2*f(a+j*h).
b) use TR[0][m-1] to obtain the remaining terms.
Initialise n (n = 0)
do
Increment n
Compute TR[n][m] from TR[n-1][m] and TR[n-1][m-1]
following eq. (5.33)
while (n<m);
while (( abs(TR[m][m]-TR[m-1][m-1]) >= ) && ( m < maxN ));
- print results: m, Tm(hm) and CV = 9R b-3
Tm(hm)
Integration - Page 19
5.8 References
M. Abramowitz and I.A. Stegun (eds.), Handbook of Mathematical Functions, (8th ed.), Dover,
New York, 1972.
J.T.S. Andrews and E.F. Westrum, J. Chem. Thermodynamics 2 (1970) 245.
C.-E. Fröberg, Introduction to Numerical Analysis, Addison-Wesley, Reading, 1966.
H. Gould and J. Tobochnik, An Introduction to Computer Simulation Methods, Part 2,
Addison-Wesley, Reading, 1988.
E. Kreyszig, Advanced Engineering Mathematics, (3rd ed.), Wiley, New York, 1972.
W.J. Moore, Physical Chemistry, (5th ed.), Longman, London, 1972.
W.H. Press, B.P. Flannery, S.A. Teukolsky and W.T. Vetterling, Numerical Recipes, (3rd ed.),
Cambridge University Press, Cambridge, 1987.
Page 20
5.9 Appendix : Function rando Location: http://www.igc.phys.ch/education/Basic/exe/num_int/ rando.cc double rando (int &ig){ /* R. GEURTSEN, GRONINGEN, WFVG, JULY 1987 (Pascal VERSION) * * REGULA WALSER, ZUERICH, JUNE 2000 (C++ VERSION) * * * * double rando (int &ig) * * * * RANDO GENERATES A RANDOM NUMBER RAND, USING A LINEAR * * CONGRUENTIAL METHOD. THE RECURSION FORMULA * * * * IRAND = MOD(IRAND * B + 1, A) * * * * IS USED WITH B = 31415821 AND A = 100000000. THE LAST * * DIGIT FROM THE RANDOM INTEGER IRAND IS CHOPPED OF, AND * * THE NUMBER IS SCALED TO A REAL VALUE RAND BETWEEN 0 AND 1, * * INCLUDING 0 BUT EXCLUDING 1. * * * * RETURN VALUE: DELIVERED WITH RANDOM NUMBER BETWEEN 0 AND 1 * * IG: RANDOM NUMBER GENERATOR SEED, IS DELIVERED * * WITH RANDOM INTEGER * * */ const int m = 100000000; const int m1 = 10000; const int mult = 31415821; int irand = abs(ig) % m; // MULTIPLY IRAND BY MULT, BUT TAKE INTO ACCOUNT THAT OVERFLOW // MUST BE DISCARDED, AND DO NOT GENERATE AN ERROR. int irandh = int(irand / m1); int irandl = irand % m1; int multh = int(mult / m1); int multl = mult % m1; irand = ((irandh*multl+irandl*multh) % m1) * m1 + irandl*multl; irand = (irand + 1) % m; // CONVERT IRAND TO A REAL RANDOM NUMBER BETWEEN 0 AND 1. double r = int(irand / 10) * 10.0; r = r / m; if ((r <= 0.0) || (r > 1.0)) r = 0.0; ig = irand; return r; } This random number generator needs a so-called seed for initialisation. The same seed will
produce the same series of random numbers. Pass an integer variable containing any number
for the seed as argument ig, and a new value will be passed back. The random number in the
interval [0,1] will be passed back as function value.