1

So I'm trying to create this program which randomly generates a 10 x 10 matrix of random integers from 0 - 2. The problem is the program always gives the same set of numbers. Here's the code:

#include <iostream>
#include <stdlib.h>

using namespace std;

int mapDraw(int array[], int mapMax, int mapBound){
    for(int i = 0; i <= (mapMax - 1); i++){
        if((i % mapBound)==0){
            cout << endl;
        }
        cout << array[i];
    }
    return 0;
}

int main(){
    int mapx = 10;
    int mapz = mapx;
    int mapArea = mapx * mapz;
    int store[mapArea];
    for(int i = 0; i <= (mapArea -1); i++){
        int height = rand() % 3;
        store[i] = height;
    }
    mapDraw(store, mapArea, mapx);
    return 0;
}

When I run the code it always gives me this:

2211210012

2210121200

0020110222

2020012110

2020020121

2210012011

1220011211

2020120002

0201021221

0021212002

For some reason it continues to give me the same out put after running the program multiple times. I would love some help and I have a hunch it will give you a different matrix when you run it on your machine so please try running it multiple times to see if the result is the same on consecutive runs. Thanks for the help in advance!

Community
  • 1
  • 1
LoreleiRS
  • 111
  • 2
  • 2
  • 6

4 Answers4

10

Call srand() with a parameter that changes for each different run of the program (typically time(0) works fine). This will initialize the internal state of the pseudo-random number generator (which, as you noticed, is set to a default state whenever a program runs).

user3146587
  • 4,250
  • 1
  • 16
  • 25
6

This is not a bug, it's a feature.

You need this to be repeatable in order to debug.

What you should do once you have it working is pass a seed value (e.g. current time in millis). That'll make it unique from run to run.

duffymo
  • 305,152
  • 44
  • 369
  • 561
4

You need to seed the random number generator, otherwise it will always produce the same sequence of numbers. The interface to do this is srand. Of course, you need a random number as input to srand. You have to get that somewhere. Depending on your larger goals, time(0) may be good enough, or it may be completely unacceptable (but then probably rand itself is not good enough, too). Tell us what this is for and we can give better advice.

zwol
  • 135,547
  • 38
  • 252
  • 361
1

You have to randomize the seed of the random number generator, you do that with srand(); e.g use srand (time(NULL)) before you call rand().

There are different algorithms for random number generators. The one most often used nowadays is a Mersene twister. It is good to take control over you uniform random number generator if you are e.g. building a Monte Carlo simulator to check reproduction of results. You can use following commands:

#include <random>
std::mt19937 m_Twister;
m_Twister(time(NULL));
std::uniform_real_distribution<double> distribution(0.0, 1.0);
//uniform random between [0,1]
double random = distribution(m_Twister);

Another algotihm is the Park Miller, it is a bit outdated nowadays though.

user3116431
  • 293
  • 1
  • 10