Fill a matrix randomly

Solved
AmiraSMI Posted messages 2 Status Member -  
fiddy Posted messages 441 Registration date   Status Contributor Last intervention   -
Good evening everyone
please I want to fill a matrix in a random way but only with these three values 0, 1 and -1
I tried this code but it only displays 0 and 1
Please help me it's urgent
 #include<stdlib.h> #include<stdio.h> #include<time.h> #define M 100 int main(){ int A[M][M]; int i,j; //generate matrix for(i=0;i<10;i++){ for(j=0;j<10;j++){ A[i][j]=rand()%2; } } //display matrix printf("\nMatrix :\n"); for ( i=0; i<10; i++ ){ for ( j=0; j<10; j++ ){ A[0][j]=0; printf("%d\t",A[i][j]); } printf("\n"); } printf("\n"); return 0; } 

1 answer

  1. fiddy Posted messages 441 Registration date   Status Contributor Last intervention   1 847
     
    Hello,

    Just draw a number between 0 and 3 and subtract 1.

    rand()%2;
    rand()%3 -1;


    Regards,

    Google is your friend
    0