# Creating multiple graphs with TMultigraph for N Graphs

**URL:** <https://root-forum.cern.ch/t/creating-multiple-graphs-with-tmultigraph-for-n-graphs/46647>\
**Category:** Newbie\
**Tags:** root\
**Created:** [September 2, 2021, 10:12pm UTC](https://root-forum.cern.ch/t/creating-multiple-graphs-with-tmultigraph-for-n-graphs/46647 "2021-09-02T22:12:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JChacon](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@JChacon](https://root-forum.cern.ch/u/JChacon)\
**Post date:** [September 2, 2021, 10:12pm UTC](https://root-forum.cern.ch/t/creating-multiple-graphs-with-tmultigraph-for-n-graphs/46647/1 "2021-09-02T22:12:54Z")

</div>

Hello, kind of new user here so apologies if question is naive.

I’m Using root for creating a graph of random walks, so far a simple random walk works and I can create a graph for the code, my question comes to how do I scale it to show multiple number of walks using TMultiGraph()

I’m also quite rusty in C++ so if I have to make a for or while loop to declare the walker, that’s something that I don’t know how to do it.

Here’s the code I wrote for it and the graph it creates.

```auto
// Random Walk 
#import <time.h>
{
  g = new TGraph();

  int i, y=1;
  srand(time(NULL));
  double p = 0.5;
    for(int i=0; i<=100; i++){
      if (rand()%2<p){
	y +=1;
        }
    else{
      y +=-1;
    }      
    g->SetPoint(i,i,y);
  }
    g->Draw("AL*");
  }
  

```

Thanks in advance

 ![random_walk](https://root-forum.cern.ch/uploads/default/original/3X/9/6/9673474f2deecf1d632db28c08867520bb085ef8.png)

_ROOT Version:_ 6.24.02  
_Platform:_ Ubuntu  
_Compiler:_ C++

* * *

---

<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:** [September 3, 2021, 6:06am UTC](https://root-forum.cern.ch/t/creating-multiple-graphs-with-tmultigraph-for-n-graphs/46647/2 "2021-09-03T06:06:40Z")

</div>

```cpp
{
  TMultiGraph *mg = new TMultiGraph();
  srand(time(NULL));
  for (int j = 0; j < 8; j++) {
    TGraph *g = new TGraph();
    double p = 0.5;
    int y = 1;
    for(int i = 0; i <= 100; i++) {
      if (rand()%2 < p) y += 1; else y -= 1;
      g->SetPoint(i, i, y);
    }
    g->SetLineColor(j + 1); g->SetMarkerColor(j + 1);
    mg->Add(g);
  }
  mg->Draw("AL*");
}

```
