Get Segmentation Fault Occasionally
<p>I am trying to make a random number generator for a uni project.</p>

<p>I am trying to implement a function to get the period of this list of numbers generated (how long before the numbers start to repeat).</p>

<p>When I compile this and run it  it returns 507 (which is the correct period).</p>

<p>Yet about 50% of the time it returns a Segmentation Fault.
Any idea what is happening: </p>

<pre><code>#include&lt;iostream&gt;
#include&lt;fstream&gt;
#include&lt;vector&gt;
#include&lt;math.h&gt;
#include&lt;ctime&gt;

using namespace std;

class Random
{
public:
// Constructor
Random(int a, int b, int m)
{
    this-&gt;setMagicNumbers(a,b,m);
}

// Function to set the magic numbers of the random number generator
void setMagicNumbers(int a, int b, int m)
{
    this-&gt;A = a;
    this-&gt;B = b;
    this-&gt;M = m;
}

// Function that returns a list of uniformly distributed random numbers
vector&lt;double&gt; GetUniform(int N, double initialValue = time(0))
{
    double currentNumber = initialValue;
    vector&lt;double&gt; RandomNumbers;
    for(int i = 0; i &lt;= N; i++)
    {
        currentNumber = fmod((this-&gt;A*currentNumber) + this-&gt;B, this-&gt;M);
        RandomNumbers.push_back(currentNumber / this-&gt;M); // The number is normalised here
    }
    return RandomNumbers;
}

private:
int A, B, M;
};

class NumberList
{
public:
// Function to add an element to the list
void push(double x)
{
    this-&gt;v.push_back(x);
}

// Function to pop an element off the list
double pop()
{
    if(v.size() &gt; 0)
    {
        int popped = v.back();
        v.pop_back();
        return popped;
    }
    else
    {
        cout &lt;&lt; "Cannot pop element off empty list." &lt;&lt; endl;
        return 0;
    }
    return 0;
}

// Function to get the value at a given location on the list
double getAt(int i)
{
    return this-&gt;v[i];
}

    // Function to set the value at a given location on the list
void setAt(int i, double value)
{
    this-&gt;v[i] = value;
}

// Function to find the size of the list
unsigned int size()
{
    return this-&gt;v.size();
}

// Function to get the vector itself
vector&lt;double&gt; getvector()
{
    return this-&gt;v;
}

// Function to set the value of the vector itself
void setVector(vector&lt;double&gt; vec)
{
    this-&gt;v = vec;
}

// Function to print the list of numbers as coordinates to a data file
void printCoordinates()
{
        ofstream data("coordinates.dat");
        for(unsigned int i = 0; i &lt;= this-&gt;v.size(); i++)
        {
                data &lt;&lt; this-&gt;v[i] &lt;&lt;  " " &lt;&lt; this-&gt;v[i + 1] &lt;&lt;  "\n";
        }
        data.close();
}

// Function to print the list of numbers to the terminal
void print()
{
        for(unsigned int i = 0; i &lt;= this-&gt;v.size(); i++)
        {
                cout &lt;&lt; this-&gt;v[i] &lt;&lt; endl;
        }
}

// Function to get the period of the list of numbers
int getPeriod()
{
    int i = 2;
    while(true)
    {
        if(isPeriod(i) == true)
        {
            return i;
        }
        else
        {
            i = i + 1;
        }
    }
}

private:
// Vector to hold the values for the list
vector&lt;double&gt; v;

// Function to test if 'i' is the period of the list
bool isPeriod(int i)
{
    for(int j = 0; j != (i-1); j++)
    {
        if(this-&gt;getAt(j) != this-&gt;getAt(i + j))
        {
            return false;
        }
    }
    return true;
}
};

int main()
{
Random RandNumGenerator(100,104001,714025);                // Create a new random number generator with given magic numbers
NumberList numbers;                                        // Create a blank list of numbers
numbers.setVector(RandNumGenerator.GetUniform(10000));     // Add 10000 random numbers to the list
numbers.printCoordinates();                                // Print out the random numbers as coordinates to a data file
cout &lt;&lt; numbers.getPeriod() &lt;&lt; endl;                       // Print out the period of the random number list
return 0;
}
</code></pre>

<p>Thanks in advance.</p>
