# A question about Monte Carlo calculations

• zm1046838471
In summary, on page 299 of Samuel S M Wong's Computational Methods in Physics and Engineering, there is a problem with implementing Eq.(7-2) using a language such as Fortran. To avoid the problem, the code may be written in a different language. However, the modulus operation, m, must be chosen carefully to ensure good quality random integers.

#### zm1046838471

This is my first question here. I tried my best to work out it by myself but it didn't work.

I'm reading Computational Methods in Physics and Engineering by Samuel S M Wong recently.

I don't know how to deduce Eq.(7-2) on page 299 of this book. There are some paragraphs on this page as below.

Because of the wide range of applications of random numbers, computer operating systems and high-level languages are usually equipped with random number generators. A slight variant of Eq.(7-1) is often found on 32-bit machines,

Xn
+1=[(a×Xn+c)/d] mod m ------------------------------------ (7-2) //n & n+1 are the subscripts of X

with

m = 32,768 a = 1,103,515,245 c= 12,345 d = 65,536.

The range of random integers generated by this method is 0 < Xn < 2^15. There is, however, a small problem if we wish to implement Eq. (7-2) using a language such as Fortran. Since the integer a is larger than 2^30, there is a high probability that an overflow will take place on a 32-bit word length computer when it is multiplied by a random integer Xn. To avoid the problem this portion of code can be written in a different language such as C in a way that it may be called by a Fortran program.

Eq.(7-1) is Xn+1 = (a×Xn+c) mod m

It will be appreciated if you can offer some reference books that explain it clearly or give your descriptions here.

Thank you very much!

I'm so sorry I failed to upload the picture of my book. I will try some more times.

Some previous charpters are as follows

The linear congruence method The most popular way to generate random integers with a uniform distribution is the linear congruence method. In this approach, a random integer Xn+1 is produced from another one Xn through the operation

Xn+1 = (a×Xn + c) mod m ------------------ (7-1)

where the modulus, m, is a positive integer. The multiplier a and increment c are also positive integers but their values must be less than m. To start off the sequence of random integers X0, X1, X2,…, we need to input an integer m, generally referred to as the seed of the random sequence.

The spirit of Eq.(7-1) is very similar to that behind the system-clock and middle-square methods discussed above. Instead of taking the square, the seed is multiplied by an integer a, and to prevent a bad sequence from developing, an increment c is added to the product. Similar to the system-clock approach, only the least significant part of the sum (less than m) is retained through the modulus operation.

The quality of random integers generated by the linear congruence method depends critically on the values of m, a, and c selected. Because of the modulus operation, all the random integers produced by this method must be in the range [0,m-1]. For this reason, we want m to be as large as possible. On the other hand, m^2 cannot be larger than the longest integer the computer can store in its memory. It is also clear that the choices of these three quantities are related with each other. For example, in order to have a long period, it is known that

(1) c must be relative prime to m (a and m share no common factors other than 1).

(2) (a - 1) is a multiple of p, if p is a prime factor of m, and

(3) (a - 1) is a multiple of 4, if m is a multiple of 4.

For example, a possible set of values for computers with word length of 32 bits is

m = 714,025 a = 1,366 c = 150,889.

The maximum number of different random numbers that can be generated is 714,205 in this case. Other possible combinations of three integers are given in Press and coauthors[44] and detailed discussions of the considerations that must go into the choices are given in Knuth.[31]

Conversion to random numbers If instead of random integers we wish to have random (floating) numbers, we can convert an integer Xn in the interval [0, m] to a floating number Rn in interval [a, b]. For most generators, the interval is taken to be [0,1]. If the largest integer produced by the linear congruence method of Eq. (7-1) is m, any random integer Xn may be converted to a random number in the interval [0,1] by dividing Xn with m. To carry out this procedure on a computer, we must first change both Xn and m into floating numbers and then take the quotient

Rn = Xn/m.

…​

Last edited:
Can you write down the equation and provide a bit of context? You could also scan the page.

I revised my descriptions just now.

zm1046838471 said:
Xn+1=[(a×Xn+c)/d] mod m ------------------------------------ (7-2) //n & n+1 are the subscripts of X.

You can make subscripts and superscripts by highlighting the text and clicking the "x2" or "x2" button at the top of the message-editing box:

Xn+1=[(a×Xn+c)/d] mod m

[X][/n]

zm1046838471 said:
[X][/n]
What I get is [X][/n]

zm1046838471 said:
What I get is [X][/n]
If you use itex, it looks like $X_n$. Here [itx][/itx] bracket X_n, where itx is a deliberate misspelling of itex.

mathman said:
If you use itex, it looks like $X_n$. Here [itx][/itx] bracket X_n, where itx is a deliberate misspelling of itex.
Thank you!
I can get something like Xn+1=[(a×Xn+c)/d] mod m, Xn+1=[(a×Xn+c)] mod m now.

Last edited:
DrClaude said:
Can you write down the equation and provide a bit of context? You could also scan the page.
Thanks! I had revised my descriptions.

jtbell said:
You can make subscripts and superscripts by highlighting the text and clicking the "x2" or "x2" button at the top of the message-editing box:

Xn+1=[(a×Xn+c)/d] mod m

I overlooked your reply carelessly. Thank you and I get the point now.

## 1. What are Monte Carlo calculations?

Monte Carlo calculations are a type of computational algorithm that uses random sampling to solve mathematical problems. They are often used in simulations and modeling to estimate the behavior of complex systems.

## 2. How do Monte Carlo calculations work?

Monte Carlo calculations work by generating a large number of random samples from a given probability distribution and using these samples to approximate the solution to a problem. The more samples that are generated, the more accurate the approximation will be.

## 3. What are the advantages of using Monte Carlo calculations?

One of the main advantages of using Monte Carlo calculations is that they can be applied to a wide range of problems in various fields such as physics, finance, and engineering. They are also relatively simple to implement and can handle complex, multi-dimensional problems that may be difficult to solve using traditional methods.

## 4. What are the limitations of Monte Carlo calculations?

One limitation of Monte Carlo calculations is that they can be computationally expensive, especially when a large number of samples are needed to obtain an accurate result. Additionally, they may not be suitable for problems with discontinuous functions or highly correlated variables.

## 5. How can I use Monte Carlo calculations in my research?

There are many resources available online that provide step-by-step guides on how to implement Monte Carlo calculations in various programming languages. Additionally, many software packages and libraries offer built-in functions for Monte Carlo simulations. It is important to carefully consider the problem at hand and the suitability of Monte Carlo calculations before incorporating them into your research.