Afternoon:

I am trying to create a loop to give me a series of random numbers. I need the random numbers to be between 100 and 999, this is necessary so I can divide the numbers by 1000 and then use this numbers for another program. My problem is that random numbers give you numbers that are way big and not between 100 and 999. I create this code but it gives me a segmentation fault when I try to run it. Can anyone give me insight of where I made a mistake.

Thanks

G

#include <iostream>

using namespace std;

int number_generator();
int main()
{
int series;
cout << "How long you want your series: ";
cin >> series;
    

for (int i=0; i<series; i++)
        {
        number_generator();
        }
}

int number_generator()
{
double series_number;
int number_to_use = rand();
if (number_to_use > 999 && number_to_use < 1000)
        {
        cout << number_to_use << endl;
        double actual_random_integer = number_to_use/1000;
        //cout << actual_random_integer << endl;
        if (actual_random_integer < 0.25)
                {
                series_number = 1;
                }
        else
                {
                series_number = -1;
                }
        //      cout << series_number << endl;
        }
        else     
        {
        number_generator();
        }  
}

You get seg fault. because your overflowing stack.

It seems that you want a number ranging from 0.1 to 0.999

Look here

The if statement on line 23 seems problematic to me. There is no integer greater than 999 AND less than 1000.

The simplest way to adjust the range of rand() is with the modulus operator:

#include <iostream>
#include <cstdlib>

int main()
{
    for (int x = 0; x < 20; ++x)
    {
        std::cout << (std::rand() % 900 + 100) << '\n';
    }
}

I am sure someone will chime in with the multitude of problems that this solution has, but if you need to worry about those problems, rand() is not the right random number generator for your application. :)

The simplest way to adjust the range of rand() is with the modulus operator:

#include <iostream>
#include <cstdlib>

int main()
{
    for (int x = 0; x < 20; ++x)
    {
        std::cout << (std::rand() % 900 + 100) << '\n';
    }
}

I am sure someone will chime in with the multitude of problems that this solution has, but if you need to worry about those problems, rand() is not the right random number generator for your application. :)

Forgot to seed.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
    srand(time(0)); 

    for (int x = 0; x < 20; ++x)
    {
        std::cout << (std::rand() % 900 + 100) << '\n';
    }
}

Forgot to seed.

You presume that seeding is a required step, which it is not. Design choices are not the same as memory lapses. ;)

You presume that seeding is a required step, which it is not. Design choices are not the same as memory lapses. ;)

I'm intrigued. Why would you consider not seeding the RNG here?

Why would you consider not seeding the RNG here?

Why would I bother? It is an example program meant to show how the range of rand() can be shifted using modulus. Adding unnecessary code will only muddy the waters.

Why would I bother? It is an example program meant to show how the range of rand() can be shifted using modulus. Adding unnecessary code will only muddy the waters.

That's what I don't understand. Seeding seems necessary to me here. Isn't it necessary to seed the random number generator if you want good results?

Isn't it necessary to seed the random number generator if you want good results?

Not at all. The generator is seeded by default to a value of 1. It does not make the sequence returned by rand() any less random. The sequence is deterministic based on the seed, so every time the program runs with a default seed it will produce the same sequence. But the sequence itself is still pseudo random. :)

Not at all. The generator is seeded by default to a value of 1. It does not make the sequence returned by rand() any less random. The sequence is deterministic based on the seed, so every time the program runs with a default seed it will produce the same sequence. But the sequence itself is still pseudo random. :)

Ok, OP should realize this point.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.