How to generate random number without repetition?
Hello, everyone! i am trying to generate random numbers in 2d array as my code below, but i but I still have problems: At the output, i got the repetition of the index (i) .
Example:
arr[11][0] = 0.563585
arr[18][0] = 0.808741
arr[29][0] = 0.479873
arr[18][0] = 0.895962
arr[22][1] = 0.746605
arr[5][1] = 0.858943
arr[1][1] = 0.513535
arr[1][1] = 0.014985
.............................
.............................
arr[12][29] = 0.634724
arr[19][29] = 0.828791
arr[5][29] = 0.720908
arr[5][29] = 0.375134
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main(void)
{
double arr[30][30];
int m[30];
int i,j;
i=0;
for(j=0; j<30; j++){
m[j]= 0;
while(m[j] <4){
i= (int) rand()%30;
arr[i][j]= (double) rand()/RAND_MAX;
if(arr[i][j] > 0){
m[j]++;
printf("arr[%d][%d] = %lf\t", i, j, arr[i][j]);
printf("\n");
}
}
}
getch();
}
Could anyone help me?
Thanks in advance!!!