# Monte-carlo simulation of pi in root

**URL:** <https://root-forum.cern.ch/t/monte-carlo-simulation-of-pi-in-root/37382>\
**Category:** Newbie\
**Created:** [December 30, 2019, 8:10pm UTC](https://root-forum.cern.ch/t/monte-carlo-simulation-of-pi-in-root/37382 "2019-12-30T20:10:28Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![hep\_1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hep_1/32/21930_2.png) [@hep\_1](https://root-forum.cern.ch/u/hep_1)\
**Post date:** [December 30, 2019, 8:10pm UTC](https://root-forum.cern.ch/t/monte-carlo-simulation-of-pi-in-root/37382/1 "2019-12-30T20:10:28Z")

</div>

In the macro that I have attached below, I am trying to use TRandom3 to generate random numbers and then do the usual monte-carlo simulation of pi. My objective is to try and do that for different values on nTotal in order to see how the approximation of pi improves with increasing number of nTotal. I first tried the code in codeblocks (with C++ way of generating random numbers), and it works totally fine. However, this root macro does not work and gives me the following error:

> Error: Illegal initialization of nTotal. Constructor exists montecarlo2.C:6:  
> \*\*\* Interpreter error recovered \*\*\*

Could anyone help me understand what I did wrong in initializing nTotal?

[montecarlo2.C](https://root-forum.cern.ch/uploads/short-url/p7zEWFAW1ZunOZ43N1QK2ED1ZU9.C) (629 Bytes)

_ROOT Version:_ 5.10  
_Platform:_ Ubuntu

* * *

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [December 30, 2019, 8:14pm UTC](https://root-forum.cern.ch/t/monte-carlo-simulation-of-pi-in-root/37382/2 "2019-12-30T20:14:16Z")

</div>

You need ROOT 6 for this feature.

---

<div class="post-metadata">

**Author:** ![hep\_1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hep_1/32/21930_2.png) [@hep\_1](https://root-forum.cern.ch/u/hep_1)\
**Post date:** [December 30, 2019, 8:17pm UTC](https://root-forum.cern.ch/t/monte-carlo-simulation-of-pi-in-root/37382/3 "2019-12-30T20:17:22Z")

</div>

Really? That’s unfortunate cause I recently switched to Root 5 because of some issues. Any idea how I go about doing it in version 5?

---

<div class="post-metadata">

**Author:** ![meu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/meu/32/20557_2.png) [@meu](https://root-forum.cern.ch/u/meu)\
**Post date:** [December 30, 2019, 8:49pm UTC](https://root-forum.cern.ch/t/monte-carlo-simulation-of-pi-in-root/37382/4 "2019-12-30T20:49:46Z")

</div>

Hi hep\_1  
I don’t know in detail but you mistake “TRandom3 rand(0)”.  
This “rand(0)” writing is mistake. Maybe root understand rand(0) as C standard function.

I modified your code.  
try it again

```auto
{
  vector<int> nTotal = {100,1000,10000,100000,1000000};

  int circle_points = 0;
  int square_points = 0;

  TRandom3 rnd;

  for (Int_t i = 0; i < nTotal.size(); i++){
    for (Int_t j = 0; j < nTotal[i]; j++){

      Double_t x = rnd.Rndm();
      Double_t y = rnd.Rndm();

      Double_t distance = x*x + y*y;

      if (distance <= 1) circle_points++;
      square_points++;

    }
  }
  Double_t pi = double(4*circle_points)/(square_points);
    cout<<pi<<endl;
}

```

---

<div class="post-metadata">

**Author:** ![hep\_1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hep_1/32/21930_2.png) [@hep\_1](https://root-forum.cern.ch/u/hep_1)\
**Post date:** [December 30, 2019, 8:59pm UTC](https://root-forum.cern.ch/t/monte-carlo-simulation-of-pi-in-root/37382/5 "2019-12-30T20:59:13Z")

</div>

Hi @meu. I don’t think the problem is with TRandom3 rand(0) because when I ran the code for just one value of nTotal, let’s say nTotal = 1000, it was working fine. Also, the change you made did not work, and it still gives the same error. Thank you, though.

---

<div class="post-metadata">

**Author:** ![meu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/meu/32/20557_2.png) [@meu](https://root-forum.cern.ch/u/meu)\
**Post date:** [December 30, 2019, 9:31pm UTC](https://root-forum.cern.ch/t/monte-carlo-simulation-of-pi-in-root/37382/6 "2019-12-30T21:31:33Z")

</div>

> [@hep\_1](#):
>
> _ROOT Version:_ 5.10

Sorry, I can’t help.  
I used Root 6. This work good and can’t take rand(0).

So, I suggest to change vector to array.  
I think Root5 have problem with vector.

```auto
{
  int nTotal[5] = {100,1000,10000,100000,1000000};
  int circle_points = 0;
  int square_points = 0;

  TRandom3 rnd(0);

  for (Int_t i = 0; i < 5; i++){
    for (Int_t j = 0; j < nTotal[i]; j++){
    Double_t x = rnd.Rndm();
    Double_t y = rnd.Rndm();

    Double_t distance = x*x + y*y;

     if (distance <= 1) circle_points++;
     square_points++;
    }
  }
  Double_t pi = double(4*circle_points)/(square_points);
    cout<<pi<<endl;
}

```

---

<div class="post-metadata">

**Author:** ![hep\_1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hep_1/32/21930_2.png) [@hep\_1](https://root-forum.cern.ch/u/hep_1)\
**Post date:** [December 30, 2019, 9:43pm UTC](https://root-forum.cern.ch/t/monte-carlo-simulation-of-pi-in-root/37382/7 "2019-12-30T21:43:54Z")

</div>

Thank you @meu. I would like to point out that I think it should be TRandom3 rand(0) instead of rnd(0). I also have a follow-up question. So, what you suggested works perfectly fine once I changed rnd to rand. I was playing with your code and tried the following:

```auto
{
  int nTotal[6] = {100,1000,10000,100000,1000000,10000000};
  int circle_points = 0;
  int square_points = 0;

  TRandom3 rand(0);

  for (Int_t i = 0; i < 6; i++){
    for (Int_t j = 0; j < nTotal[i]; j++){
    Double_t x = rand.Rndm();
    Double_t y = rand.Rndm();

    Double_t distance = x*x + y*y;

     if (distance <= 1) circle_points++;
     square_points++;
    }
     Double_t pi = double(4*circle_points)/(square_points);
    cout<<pi<<endl;
  }
 
}

```

All I did was changed nTotal to have 6 elements instead of 5 and made corresponding changes everywhere. This one does not work, though and gives the error:

> Error: Too many initializers, exceeded length of array for ‘nTotal’ montecarloforum.C:2:  
> \*\*\* Interpreter error recovered \*\*\*

Any idea why that would be the case?

---

<div class="post-metadata">

**Author:** ![meu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/meu/32/20557_2.png) [@meu](https://root-forum.cern.ch/u/meu)\
**Post date:** [December 30, 2019, 10:26pm UTC](https://root-forum.cern.ch/t/monte-carlo-simulation-of-pi-in-root/37382/9 "2019-12-30T22:26:31Z")

</div>

In Root 6, grammar is more strict than Root5. So, I think rand(0) work in Root 5 but can’t work in Root6.

In follow-up question,I can’t see this problem.  
I think your code is correct.  
I have no idea sorry.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [December 31, 2019, 8:15am UTC](https://root-forum.cern.ch/t/monte-carlo-simulation-of-pi-in-root/37382/10 "2019-12-31T08:15:12Z")

</div>

You can use:

```cpp
  int nTotal[] = {100, 1000, 10000, 100000, 1000000, 10000000};
  // ...
  for (UInt_t i = 0; i < (sizeof(nTotal) / sizeof(nTotal[0])); i++) {

```

---

<div class="post-metadata">

**Author:** ![hep\_1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hep_1/32/21930_2.png) [@hep\_1](https://root-forum.cern.ch/u/hep_1)\
**Post date:** [December 31, 2019, 2:07pm UTC](https://root-forum.cern.ch/t/monte-carlo-simulation-of-pi-in-root/37382/11 "2019-12-31T14:07:12Z")

</div>

@Wile_E_Coyote, thank you. This works as I wanted it to. Just a small question on the for loop. So why was it necessary to divide the sizeof(nTotal) by sizeof(nTotal[0]). I thought that since sizeof(nTotal[0]) is just 1, removing that part would still allow the code to run fine. Surprisingly, I found that the code does give me six values of pi, and an error at the end saying:

> Error: Array index out of range nTotal → [7] valid upto nTotal[5] montecarloforum2.C:9:

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [January 6, 2020, 7:25pm UTC](https://root-forum.cern.ch/t/monte-carlo-simulation-of-pi-in-root/37382/12 "2020-01-06T19:25:07Z")

</div>

> [@hep\_1](#):
>
> sizeof(nTotal[0]) is just 1

It is not one. It is the same value as `sizeof(int)` which is `4 (bytes)`.

---

<div class="post-metadata">

**Author:** ![hep\_1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hep_1/32/21930_2.png) [@hep\_1](https://root-forum.cern.ch/u/hep_1)\
**Post date:** [January 7, 2020, 3:13am UTC](https://root-forum.cern.ch/t/monte-carlo-simulation-of-pi-in-root/37382/13 "2020-01-07T03:13:40Z")

</div>

@pcanal, oh okay. But still not sure why I have to do:

> i \< (sizeof(nTotal) / sizeof(nTotal[0]))

inside of that loop.
