# Template:ROOT/Exercises/Trying Interactive ROOT

Jump to navigation
Jump to search

## Exercise[edit]

On an interactive ROOT session: Fill an array of 100 000 000 64Bit floating point numbers of the machine independent ROOT type `Double_t`

with random numbers. Compute the mean of the array and print it to the console. Note the computation time.

**Hint:** You can get random real numbers by creating a pointer to an instance of the `TRandom`

class. (The constructor takes one arbitrary integer as initialization.) Then you can call the method `TRandom::Rndm()`

to get a simple pseudo random number uniformly distributed between 0 and 1. For example:

```
TRandom *R = new TRandom(time(0)); // create a pointer to a new instance of TRandom in the heap
cout << R->Rndm() << endl;
```

## Solution[edit]

root [0] Int_t nums = 100000000; // random numbers to generate |

root [1] TRandom *R = new TRandom(time(0)); |

root [2] Double_t *seed = new Double_t[nums]; |

root [3] for (Int_t i = 0; i < nums; i++) { |

end with '}', '@':abort > seed[i] = R->Rndm(); } |

root [4] // some 10-20 seconds later... |

root [5] Double_t mean = 0.0; |

root [6] for (Int_t i = 0; i < nums; i++) { // make use of pressing "uparrow" to recall the input of line 3 |

end with '}', '@':abort > mean += seed[i]; } |

root [7] // some 5-15 seconds later... |

root [8] mean /= nums; |

root [9] cout << "mean = " << mean << endl; |

mean = 0.499963 |