Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8704717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:10:42+00:00 2026-06-13T03:10:42+00:00

I am trying to make a random number generator for a uni project. I

  • 0

I am trying to make a random number generator for a uni project.

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).

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

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

#include<iostream>
#include<fstream>
#include<vector>
#include<math.h>
#include<ctime>

using namespace std;

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

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

// Function that returns a list of uniformly distributed random numbers
vector<double> GetUniform(int N, double initialValue = time(0))
{
    double currentNumber = initialValue;
    vector<double> RandomNumbers;
    for(int i = 0; i <= N; i++)
    {
        currentNumber = fmod((this->A*currentNumber) + this->B, this->M);
        RandomNumbers.push_back(currentNumber / this->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->v.push_back(x);
}

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

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

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

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

// Function to get the vector itself
vector<double> getvector()
{
    return this->v;
}

// Function to set the value of the vector itself
void setVector(vector<double> vec)
{
    this->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 <= this->v.size(); i++)
        {
                data << this->v[i] <<  " " << this->v[i + 1] <<  "\n";
        }
        data.close();
}

// Function to print the list of numbers to the terminal
void print()
{
        for(unsigned int i = 0; i <= this->v.size(); i++)
        {
                cout << this->v[i] << 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<double> 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->getAt(j) != this->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 << numbers.getPeriod() << endl;                       // Print out the period of the random number list
return 0;
}

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:10:44+00:00Added an answer on June 13, 2026 at 3:10 am

    This line could cause a problem

    for(unsigned int i = 0; i <= this->v.size(); i++)
    

    With it you are using size as the last index, which is overflow, try changing it for this:

    EDIT: change for this actually

    for(unsigned int i = 0; i < this->v.size()-1; i++)
    

    On the loop you are accessing the i+1 th element of the vector.

    EDIT:

    This probably wouldn’t cause a crash but instead of creating N elements you are creating N+1

    for(int i = 0; i <= N; i++)
    

    Change if for this:

    for(int i = 0; i < N; i++)
    

    EDIT:
    With fixes

    #include<iostream>
    #include<fstream>
    #include<vector>
    #include<math.h>
    #include<ctime>
    
    using namespace std;
    
    class Random
    {
    public:
    // Constructor
    Random(long a, long b, long m)
    {
        this->setMagicNumbers(a,b,m);
    }
    
    // Function to set the magic numbers of the random number generator
    void setMagicNumbers(long a, long b, long m)
    {
        this->A = a;
        this->B = b;
        this->M = m;
    }
    
    // Function that returns a list of uniformly distributed random numbers
    vector<double> GetUniform(int N, double initialValue = time(0))
    {
        double currentNumber = initialValue;
        vector<double> RandomNumbers;
        for(int i = 0; i < N; i++)
        {
            currentNumber = fmod((this->A*currentNumber) + this->B, this->M);
            RandomNumbers.push_back(currentNumber / this->M); // The number is normalised here
        }
        return RandomNumbers;
    }
    
    private:
    long A, B, M;
    };
    
    class NumberList
    {
    public:
    // Function to add an element to the list
    void push(double x)
    {
        this->v.push_back(x);
    }
    
    // Function to pop an element off the list
    double pop()
    {
        if(v.size() > 0)
        {
            int popped = v.back();
            v.pop_back();
            return popped;
        }
        else
        {
            cout << "Cannot pop element off empty list." << endl;
            return 0;
        }
        return 0;
    }
    
    // Function to get the value at a given location on the list
    double getAt(int i)
    {
        return this->v[i];
    }
    
        // Function to set the value at a given location on the list
    void setAt(int i, double value)
    {
        this->v[i] = value;
    }
    
    // Function to find the size of the list
    unsigned int size()
    {
        return this->v.size();
    }
    
    // Function to get the vector itself
    vector<double> getvector()
    {
        return this->v;
    }
    
    // Function to set the value of the vector itself
    void setVector(vector<double> vec)
    {
        this->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 < this->v.size()-1; i++)
            {
                    data << this->v[i] <<  " " << this->v[i + 1] <<  "\n";
            }
            data.close();
    }
    
    // Function to print the list of numbers to the terminal
    void print()
    {
            for(unsigned int i = 0; i < this->v.size(); i++)
            {
                    cout << this->v[i] << 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<double> v;
    
    // Function to test if 'i' is the period of the list
    bool isPeriod(int i)
    {
        std::cout << "trying period " << i << endl;
        if (i >= v.size()) return true;
        for(int j = 0; j < v.size()-1; j++)
        {
            if(this->getAt(j) == this->getAt(i + j))
            {
                std::cout << "value at j " << this->getAt(j) << "value at i+j " << this->getAt(i+j) <<endl;
                return true;
            }
        }
        return false;
    }
    };
    
    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 << numbers.getPeriod() << endl;                       // Print out the period of the random number list
    return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a random number to be a palindrome. For example, generated
I am trying to make a script to pick random number between two numbers
I m trying to make a random number generator in android. My code has
I'm trying to make a simple solitaire in wich you get a random number
I'm trying to create a random number generator that generates random numbers between two
I am trying to make a Java implementation of the Park-Miller-Carta PRNG random number
I'm trying to make a basic encryption program that used random numbers to encrypt
I am trying to make a constant random number generators (I mean a RNG
i'm trying to create a random number generator that will generate number based on
I'm trying to make a simple javascript application to pick a random number between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.