Using GSL on RHUL Particle Physics computers

GNU scientific library (GSL)

This is a numerical library for use with C or C++ code. The library covers all the main numerical needs of a physicist and in particular the range of random number generators is very good.

The library is not written in an object oriented style since it can also be used with C, however I have assumed here that you will be writting code in C++.

So far myself, I have made use of the random number generators, the non-MC integration, interpolation and ODE solving routines so might be able to help if you have trouble with these sections.

How to

The reference manual is quite good (I recommend you look at it!) with examples for every section, demonstrating how to write the source code (the .cpp file) for a program that uses GSL, so the main information you need to make use of GSL at Royal Holloway is how to compile and run the code you've written.


To compile your program it's probably best to use this GNUmakefile. Save it in the same directory as your program files, alter it where indicated to have your file names, then type gmake on the terminal commmand line (whilst in that same directory).


GSL is in the directory/usr/local/lib so when about to run your program you must type

setenv LD_LIBRARY_PATH /usr/local/lib
on the command line of your terminal so the program can find it when run.

For random numbers there are some additional environment variables to be set. This means that you can write your program and compile it, then only just before you run the program do you decide which random number generator to use and set the seed, which are done by typing

setenv GSL_RNG_SEED 123
setenv GSL_RNG_TYPE mrg
on the command line of your terminal. Where 123 is the integer you wish to be the seed and mrg is the name of the random number generator - see GSL manual for choices of generator. This is potentially very useful as you can run your program many times with different generators or seeds without needing to recompile.

Rather than worrying about remembering to do these things before running my program I have them in a short shell script which I run instead. Here it is: To run it type ./ in the same directory as and your program. (NB: before you first use remember to change the permissions so the computer understands it's an executeable: chmod +x


If using the more recent version of g++ by sourcing gcc3.2-setup.csh and/or ROOT by sourcing something like be careful to do setenv LD_LIBRARY_PATH /usr/local/lib before sourcing these scripts. This is since both scripts also set or prepend to LD_LIBRARY_PATH and that information would be overwritten. The best thing is to remove setenv LD_LIBRARY_PATH /usr/local/lib from the and put it in a setup script along with a sourcing of the g++ or root setups as required, eg:

Failure to do this results in the necessary libraries not being found at runtime.

Try the online GSL manual or I have a full printed version in my office you are welcome to use.

Random Number Example

/* Author:  Clare Quarman
 * Date:    July 2003
 * Purpose: demonstrate use of GSL random no generator in a c++ program

// things i like to use
#include <iostream>
#include <cstdlib>
#include <cmath>
using std::cout;
using std::cin;
using std::endl;
using std::cerr;

// GSL thing for random numbers
#include <gsl/gsl_rng.h>

gsl_rng * r;  // pointer to a global random no generator

int main (){

  // have to do these things first - they are explained in manual
  const gsl_rng_type * T;
  T = gsl_rng_default;
  r = gsl_rng_alloc (T);

  // get a random number from a uniform [0,1) distribution
  double u = gsl_rng_uniform (r);

  if (u<0.5){
    cout << "heads" << endl;
    cout << "tails" << endl;

  // at end of program must get rid of memory used by the generator
  gsl_rng_free (r);

  return EXIT_SUCCESS;

All the files needed for this example tossCoin.cpp, GNUmakefile and are here


GSL is freely available for a range of platforms if you'd like it for you home computer or laptop - see

go to Clare's homepage

Clare Quarman
Last modified: Wed Mar 17 18:26:45 GMT 2004