1
$\begingroup$

I'm trying to learn the implementation of the Monte Carlo method in R language. I am following a course with an example of predicting results for a board game. What I'm not understanding is the usage of a function that was implemented on the top of the code, and that is:

set.seed(5678)

Do you know what this function is used for and the meaning of the "5678" numbers inside the brackets?

$\endgroup$

3 Answers 3

1
$\begingroup$

When you wonder what a library function does, you should look it up. Get into the habit of reading the language or library's documentation. This can be more or less helpful depending on the quality of the documentation, but when the quality is good, that's your problem solved.

In this particular case, the reference documentation is admittedly pretty hard to follow. Searching for “set.seed R” on the web finds some good articles, e.g. 1 2 3 4.

That being said, let me answer the on-topic aspect of the question, which is how seeding the random generator (which is what set.seed does) relates to the Monte-Carlo method.

A Monte Carlo method is an algorithm that uses random sampling to make approximate calculations. It's used when doing the full calculation with all the data would involve such a high volume of data that the calculation would be impossible in practice. So instead we do the calculation with a small subset of the data. This can only work if the subset is representative of the data.

To get a representative subset, we take data points at random. Statistically, in most physical systems, even a moderate amount of data points (e.g. thousands out of billions) are enough to get reasonably accurate results.

For programming, the downside of a random selection is that if you run the program twice, you'll get different results. The results should be approximately the same if the Monte Carlo method is suitable for the problem that's being solved, but they won't be identical. For example, you can't double-check someone else's calculation.

So instead of using an actual random selection, the program uses a pseudorandom selection. A pseudorandom generator (PRNG) is a source of numbers that is reproducible, but when you only look at the numbers, they're indistinguishable from a truly random source. (There are different levels of quality to the indistinguishability — for example cryptography requires a higher level of indistinguishability than Monte Carlo simulations.) The seed of a PRNG is an input to the generator, such that if you run the PRNG twice with the same seed, you get the same outputs. But if you run different seeds, you get different, independent outputs. A pseudorandom generator works just as well as a random generator because it's independent (in the probabilistic sense, but I'm not going to formalize this) from anything else, including the phenomenon you're modelling. (It's also independent from the same PRNG with a different seed.)

There is no significance to the value 5678. Any number would do (at least any number within the permissible range for that particular library function). You can use a different value and you'll get results that are different, but statistically equally representative of the phenomenon.

$\endgroup$
1
$\begingroup$

Unless a computer has special hardware to generate truly random numbers, it can only generate pseudo-random numbers. Pseudo-random means that the numbers seem random but are in reality generated by a deterministic algorithm. So they will always follow the same sequence of numbers, periodically (though the period can be absolutely astronomical*).

To avoid always starting the random numbers at the same point in the period, seeding is used, meaning that you supply a number that changes the starting point. The seed is arbitrary, but it is up to you to keep the same or change it every time, depending on your needs.


*In the old days, there were random generators with a period as small as 32767 or so, which is considered tiny. Nowadays, you can find generators with a period longer than the number of quarks in the universe.

$\endgroup$
0
$\begingroup$

A random seed is a number that is used to initialize a pseudorandom number generator. The resulting numbers that are generated will depend on the seed.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.