0

i was working on a simple idea of generating random numbers (from 1 to 100) and printing them on the console using c++ but the problem is that each time i run the program it geberates exactly the same numbers each time, it's random, a single number isn't repeated but the same order of random numbers every time!

here's my code

// DigitDisplayer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h" 
#include <iostream>
#include <cstdlib>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int max = 100;
    int min = 1;
    int range = (max - min) + 1;
int randnum;
bool nums[100];
for(int j = 0; j < sizeof(nums); j++)
{
    nums[j] = false;
}
for(int i = 0; i < range; i++)
{
    int randnum = (rand() % range) + 1;
    if(nums[randnum] == false){
        nums[randnum] = true;
        cout<< randnum << "\n\n";
    }
    else {
        int randnum = (rand() % range) + 1;
    }
}
cout<< "DONE!!!\n\n";
system ("pause");
}

Thanks

  • 3
    You should initialize the random number generator using `srand()` at the beginning of your program. – Cyclonecode Feb 26 '13 at 14:44
  • As an aside, if you're using the precompiled header mechanism the idea is you put iostream and cstdlib inside stdafx.h then the compiler can reuse the loaded headers across compiles. – Rup Feb 26 '13 at 14:48

4 Answers4

1

The random number generator in C++ is actually a pseudo-random sequence generator, that starts from a given seed.

This is done for two reasons:

  • it is very difficult to find a true source of random on all machines implementing the C++ library.

  • there is a clear need for a random number generator that generates repeatable sequences (for example, if your unit tests rely on random numbers, you want the tests to be repeatable - and receive the same "random numbers").

That is why you have the srand function that provides a "seed" to the random number generation.

If you do not call srand or if you call it with the same value, your "random" sequence will always be the same.

If you call it with a varying seed, the resulting sequence will be unique (in practice it probably has a pattern or is predictable - that depends on the implementation).

utnapistim
  • 26,809
  • 3
  • 46
  • 82
0

You must use srand to seed rand function, otherway it always return same set of numbers. time() function defined in cstdlib

check this:

// DigitDisplayer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h" 
#include <iostream>
#include <cstdlib>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int max = 100;
    int min = 1;
    int range = (max - min) + 1;
    int randnum;
    bool nums[100];

     srand (time(NULL));
for(int j = 0; j < sizeof(nums); j++)
{
    nums[j] = false;
}
for(int i = 0; i < range; i++)
{
    int randnum = (rand() % range) + 1;
    if(nums[randnum] == false){
        nums[randnum] = true;
        cout<< randnum << "\n\n";
    }
    else {
        int randnum = (rand() % range) + 1;
    }
}
cout<< "DONE!!!\n\n";
system ("pause");
}
Boynux
  • 5,958
  • 2
  • 21
  • 29
0

You need to seed the ranom number generator. Check out srand() for how to do that.

All you usually have to do is call srand() once somewhere before you use rand(). Note that you should call srand() with a different value each time you run your program.

For example, using time() to seed is the easiest way:

int main()
{
     srand(time(NULL));
     .....
     rand();
     .....
}
Daniel Dinu
  • 1,783
  • 12
  • 16
0

Computers can't get generate random numbers. Instead they use an algorithm to generate series of numbers that approximate the way that random numbers behave. These algorithms are called psuedo-random number generators (PNRGs). In every case I know of, these PNRGs can be 'seeded' with a number, this determines where the sequence of pseudo-random numbers will begin. If you want to get different results each time you need to seed your PNRG using a number that varies. For most purposes, the system timer will suffice to generate an effectively random series of numbers. srand(time(NULL)) will do this for you, as others have explained.

By the way: the built-in random number generator in C is terrible. It is not fit for use in all but the most casual of settings; if you want a decent source of random numbers consider using something like the Mersenne Twister. Seeding from time can also have problems under highly sensitive situations (e.g. online gambling, etc.).

Jack Aidley
  • 19,439
  • 7
  • 43
  • 70