Linux, CentOS, Redhat G++ compiler and rand() / random function

If you are using the G++ compiler in Linux, CentOS, or Redhat there are a couple of steps to adding a random number generator to any project.

  1. Include the precompile library: #include <stdlib.h>
  2. Use the function and add seeding: first_number = rand()%6 +1);

The basic function in C++ for the G++ compiler in Linux looks like this:

#include <iostream>

#include <stdlib.h>

int main()


int first_number = rand()%6 +1;

std::cout << first_number;

return (0);


Comments are closed.