linux生成随机矩阵
时间: 2024-05-08 21:13:37 浏览: 10
Linux系统可以使用rand()函数生成随机数,可以利用这个函数来生成随机矩阵。以下是一个简单的例子:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int rows, cols, i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int matrix[rows][cols];
srand(time(NULL)); // 初始化随机数种子
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
matrix[i][j] = rand() % 100; // 生成0-99之间的随机数
}
}
printf("The generated random matrix is:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
```
该程序会要求用户输入矩阵的行数和列数,然后利用rand()函数生成0-99之间的随机数填充矩阵,并输出生成的矩阵。