0

I'm trying to use the GNU Scientific Library and am having trouble understanding its documentation. Here's the sample program from the page on gsl_rng_env_setup:

#include <stdio.h>
#include <gsl/gsl_rng.h>

gsl_rng * r;  /* global generator */

int
main (void)
{
  const gsl_rng_type * T;

  gsl_rng_env_setup();

  T = gsl_rng_default;
  r = gsl_rng_alloc (T);

  printf ("generator type: %s\n", gsl_rng_name (r));
  printf ("seed = %lu\n", gsl_rng_default_seed);
  printf ("first value = %lu\n", gsl_rng_get (r));

  gsl_rng_free (r);
  return 0;
}

My problems start with the third line, gsl_rng * r. This is clearly not multiplication (neither variable defined yet), so it must be pointer notation. However from the C++ tutorial on pointers, I would expect something like gsl_rng = *r, which would take the value of r and store that as gsl_rng. My guess is that gsl_rng isn't a variable, but some GNU Scientific library command; however I don't understand the documentation page for it also: this command is clearly not of the form gsl_rng * gsl_rng_alloc (const gsl_rng_type * T) - even if r = gsl_rng_alloc, this command doesn't have brackets.

It doesn't help that a bit further down we have the line const gsl_rng_type * T which is of the same form but also clearly does something different. This line seems to be defining gsl_rng_type as a constant, and assigning it the value of *T - but this is missing an assignment operator. Yet T must be a variable, since a few lines later it's assigned the value of gsl_rng_default ...

My problems seem to be extremely basic which is troubling. Can anyone point me in the right direction?

king_nak
  • 11,313
  • 33
  • 58
Allure
  • 261
  • 1
  • 9

2 Answers2

2

gsl_rng is a type. The statement gsl_rng * r; declares a global variable named r with type pointer to gsl_rng. Later, there is this line r = gsl_rng_alloc (T);, which assigns some value to that declared variable.

This is basic C++, so maybe you should start with some good book, if you want to understand such code.

Jaa-c
  • 5,017
  • 4
  • 34
  • 64
  • Ironically I've already written long complex C++ programs before, but have never encountered this syntax. I'm taking your answer to mean that gsl_rng is no different from int, double, float, and so on.Thanks! – Allure Jan 11 '18 at 10:39
  • 2
    I would agree with @Allure that this is not 'basic' C++ at all. I've taught C++. – nicomp Jan 11 '18 at 10:59
  • 1
    Personally, I would characterise the GSL and that code as `C` rather than `C++`. – Mark Setchell Jan 11 '18 at 11:13
1

The trick is to remember that there are different kinds of random number generators. Each one is its' own class. The gsl_rng_alloc method will create a random number generator object for you but wants to know what class to use. You tell it what class to use by passing the class. Then the method uses that class to instantiate an object for you. It returns to you a pointer to the object that it created for you.

nicomp
  • 4,344
  • 4
  • 27
  • 60