dimanche 6 novembre 2016

Random Array C++ [duplicate]

This question already has an answer here:

RANDOM NUMBER IN ARRAY C++

Hi, when I try to generate random numbers into the array I get the same values, I am trying to generate the random numbers in a function.

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

using namespace std;

double random();

int main()
{
const int X = 15;
const int Y = 10;
double arr[X][Y];
int x, y;

for (y = 0; y < Y; y++)
{
    for (x = 0; x < X; x++)
    {
        arr[x][y] = random();
    }
}

for (y = 0; y < Y; y++)
{
    for (x = 0; x < X; x++)
    {
        cout << arr[x][y] << " ";
    }
    cout << endl;
}

cin.get();
cin.get();
return 0;
}

double random()
{
const int min = -999;
const int max = 999;
double value;

unsigned seed = time(0);
srand(seed);

return value = (rand() % (max - min + 1)) + min;
}

If anyone could explain, thanks




Aucun commentaire:

Enregistrer un commentaire