How to put random numbers into an array using C language?
In C language, you can use the rand() function to generate random numbers and then put them into an array. Below is an example code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10
int main() {
int arr[SIZE]; // 声明一个大小为SIZE的数组
int i;
srand(time(NULL)); // 使用当前时间作为随机数生成器的种子
// 生成随机数并放入数组中
for(i = 0; i < SIZE; i++) {
arr[i] = rand() % 100; // 生成0-99之间的随机数
}
// 打印数组中的随机数
for(i = 0; i < SIZE; i++) {
printf("%d ", arr[i]);
}
return 0;
}
In the example code above, we first declare an integer array ‘arr’ of size SIZE. Then we use the srand() function and time() function to use the current time as a seed, ensuring that different random numbers are generated each time the program is run. Next, we use a for loop to generate random numbers one by one and place them in the array. Finally, we use another for loop to print the random numbers in the array.
To use the rand() function, make sure to include the