#include <iostream>
#include <math.h>
usingnamespace std;
int main(void){
int m[50][50];
int counter = 0; // number of -10.
float P = 0;
for(int i=0;i<50;i++)
for(int j=0;j<50;j++){
m[i][j] = rand() % 10 + 1;
if (m[i][j] > 6){
m[i][j] = -10;
counter++ ;
}
}
P = counter*100/pow(50,2);
cout << "The number of -10 is " << counter << "\n-10 Percent is " << P ;
cout << "\n";
system("PAUSE");
return 0;
}
I want for example the 68% of the elements in the matrix m[50][50] to be -10 so what i do now is to change the line 13 until i have the 68% . Any idea, how can i achieve this randomly(without changing any line by my own) ?
Also i would like to know if i use correctly the rand() function, because i think that it isnt random at all, it generates the same numbers...
If you want to have exactly 25*P numbers equal to -10, fill first 25*P positions in the array with -10, fill the rest with [1;10] and then shuffle the array.
rand() returns pseudo random numbers. Each number returned is a function of the previous number. Each time you start your application, the initial value is the same, thus all other values are the same as well. To avoid this, you need a somewhat random number to start the sequence. This is usually time. See http://www.cplusplus.com/reference/clibrary/cstdlib/srand/