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 toThe 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. CompilingTo 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). RunningGSL is in the directory/usr/local/lib so when about to run your program you must type
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
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: run.sh. To run it type ./run.sh in the same directory as run.sh and your program. (NB: before you first use run.sh remember to change the permissions so the computer understands it's an executeable: chmod +x run.sh.) Update!If using the more recent version of g++ by sourcing
gcc3.2-setup.csh and/or ROOT by
sourcing something like rootSetup.sh be
careful to do ManualTry 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; gsl_rng_env_setup(); 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; }else{ 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 run.sh are here AvailabilityGSL is freely available for a range of platforms if you'd like it for you home computer or laptop - see http://sources.redhat.com/gsl/ |