linux srand
时间: 2023-10-01 13:04:53 浏览: 33
在 Linux 系统中,srand() 函数被用于设置伪随机数生成器的种子。它需要一个整数作为参数,通常使用时间值作为种子,以确保每次程序运行时生成的随机数序列都是不同的。srand() 函数应该在调用 rand() 函数之前被调用一次。
以下是一个示例程序,演示了如何使用 srand() 和 rand() 函数生成随机数:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i, n;
// 使用当前时间作为随机数生成器的种子
srand(time(NULL));
// 生成并输出 10 个随机数
for (i = 0; i < 10; i++)
{
n = rand();
printf("%d\n", n);
}
return 0;
}
```
注意,由于 rand() 函数生成的是伪随机数,因此不能用于安全相关的应用。如果需要高质量的随机数,应该使用专门的随机数库,如 OpenSSL 库。
相关问题
linux生成随机矩阵
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之间的随机数填充矩阵,并输出生成的矩阵。
Linux c实现贪吃蛇
以下是Linux C语言实现贪吃蛇的基本步骤:
1. 定义蛇的结构体,包括蛇头、蛇身和蛇尾等信息。
2. 初始化蛇的位置和长度。
3. 定义食物的结构体,包括食物的位置和分值等信息。
4. 随机生成食物的位置。
5. 定义蛇的移动函数,包括蛇头的移动、蛇身的移动和蛇尾的删除等操作。
6. 定义蛇的死亡判断函数,包括蛇头是否撞墙和蛇头是否撞到蛇身等情况。
7. 定义游戏主循环,包括监听用户输入、移动蛇、判断蛇是否死亡、更新分数和重新生成食物等操作。
8. 在游戏结束后,清理蛇和食物的数据。
以下是一个简单的Linux C语言实现贪吃蛇的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <termios.h>
#define ROW 20
#define COL 20
struct snake {
int row;
int col;
struct snake *next;
};
struct food {
int row;
int col;
int score;
};
struct snake *head = NULL;
struct food food;
void init_snake() {
struct snake *p1 = (struct snake *)malloc(sizeof(struct snake));
p1->row = 5;
p1->col = 5;
p1->next = NULL;
head = p1;
struct snake *p2 = (struct snake *)malloc(sizeof(struct snake));
p2->row = 5;
p2->col = 6;
p2->next = NULL;
p1->next = p2;
}
void init_food() {
srand(time(NULL));
food.row = rand() % ROW;
food.col = rand() % COL;
food.score = 10;
}
void draw() {
system("clear");
printf("score: %d\n", food.score);
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (i == food.row && j == food.col) {
printf("*");
} else {
struct snake *p = head;
int flag = 0;
while (p != NULL) {
if (p->row == i && p->col == j) {
printf("#");
flag = 1;
break;
}
p = p->next;
}
if (flag == 0) {
printf(" ");
}
}
}
printf("\n");
}
}
void add_snake_node(int row, int col) {
struct snake *p = (struct snake *)malloc(sizeof(struct snake));
p->row = row;
p->col = col;
p->next = head;
head = p;
}
void delete_snake_node() {
struct snake *p = head;
while (p->next->next != NULL) {
p = p->next;
}
free(p->next);
p->next = NULL;
}
void move_snake(int direction) {
int row = head->row;
int col = head->col;
switch (direction) {
case 1:
row--;
break;
case 2:
row++;
break;
case 3:
col--;
break;
case 4:
col++;
break;
default:
break;
}
add_snake_node(row, col);
if (row == food.row && col == food.col) {
food.score += 10;
init_food();
} else {
delete_snake_node();
}
}
int snake_die() {
if (head->row < 0 || head->row >= ROW || head->col < 0 || head->col >= COL) {
return 1;
}
struct snake *p = head->next;
while (p != NULL) {
if (p->row == head->row && p->col == head->col) {
return 1;
}
p = p->next;
}
return 0;
}
int getch() {
struct termios tm, tm_old;
int fd = STDIN_FILENO, c;
if (tcgetattr(fd, &tm) < 0) {
return -1;
}
tm_old = tm;
cfmakeraw(&tm);
if (tcsetattr(fd, TCSANOW, &tm) < 0) {
return -1;
}
c = getchar();
if (tcsetattr(fd, TCSANOW, &tm_old) < 0) {
return -1;
}
return c;
}
int main() {
init_snake();
init_food();
int direction = 2;
while (1) {
draw();
int c = getch();
if (c == 'w' && direction != 2) {
direction = 1;
} else if (c == 's' && direction != 1) {
direction = 2;
} else if (c == 'a' && direction != 4) {
direction = 3;
} else if (c == 'd' && direction != 3) {
direction = 4;
}
move_snake(direction);
if (snake_die()) {
printf("Game Over!\n");
break;
}
usleep(200000);
}
return 0;
}
```