在linux系统中写了一个C语言的文件,在里面写一个随机生成随机数
时间: 2024-03-20 11:42:05 浏览: 23
你可以使用C标准库中的rand()函数来生成随机数,需要先使用srand()函数初始化随机种子。以下是一个示例程序,可以生成随机数并将其写入文件中:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int i, num;
FILE *fp;
fp = fopen("random.txt", "w"); // 打开文件,以写入方式打开
srand(time(NULL)); // 初始化随机种子
for(i = 0; i < 10; i++) {
num = rand() % 100; // 生成0~99之间的随机数
fprintf(fp, "%d\n", num); // 将随机数写入文件中
}
fclose(fp); // 关闭文件
return 0;
}
```
这个程序将在当前目录下创建一个名为"random.txt"的文件,其中包含10个0到99之间的随机整数。你可以根据自己的需要更改生成的随机数数量和范围。
相关问题
可以帮我写一个机遇linux系统的c语言小游戏吗,小游戏是俄罗斯方块
当然可以,俄罗斯方块是一个比较经典的小游戏,也非常适合用C语言来实现。下面是一个简单的俄罗斯方块游戏的代码实现,适用于Linux系统。
首先,我们需要用到ncurses库来实现游戏的界面显示和用户输入响应。请确保您的Linux系统中已经安装了ncurses库,如果没有请使用以下命令来安装:
```
sudo apt-get install libncurses5-dev libncursesw5-dev
```
接下来,就是游戏的实现代码了。请参考以下代码:
```c
#include <ncurses.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 20
#define COLS 10
#define BLOCK_SIZE 4
#define EMPTY 0
#define WALL 1
#define BLOCK 2
int map[ROWS][COLS];
int block[BLOCK_SIZE][BLOCK_SIZE];
int block_color;
int block_x, block_y;
void init()
{
srand(time(NULL));
initscr();
cbreak();
noecho();
curs_set(0);
keypad(stdscr, TRUE);
timeout(1000);
start_color();
init_pair(WALL, COLOR_WHITE, COLOR_WHITE);
init_pair(BLOCK, COLOR_CYAN, COLOR_BLACK);
}
void draw_block(int x, int y)
{
int i, j;
attron(COLOR_PAIR(block_color));
for (i = 0; i < BLOCK_SIZE; i++) {
for (j = 0; j < BLOCK_SIZE; j++) {
if (block[i][j] != EMPTY) {
mvprintw(y + i, x + j * 2, "[]");
}
}
}
attroff(COLOR_PAIR(block_color));
}
void erase_block(int x, int y)
{
int i, j;
for (i = 0; i < BLOCK_SIZE; i++) {
for (j = 0; j < BLOCK_SIZE; j++) {
if (block[i][j] != EMPTY) {
mvprintw(y + i, x + j * 2, " ");
}
}
}
}
void new_block()
{
int i, j;
block_color = rand() % 7 + 1;
for (i = 0; i < BLOCK_SIZE; i++) {
for (j = 0; j < BLOCK_SIZE; j++) {
block[i][j] = rand() % 2;
}
}
block_x = (COLS - BLOCK_SIZE) / 2;
block_y = 0;
if (map[block_y][block_x] != EMPTY) {
endwin();
exit(0);
}
draw_block(block_x * 2, block_y);
}
int is_valid(int x, int y)
{
int i, j;
for (i = 0; i < BLOCK_SIZE; i++) {
for (j = 0; j < BLOCK_SIZE; j++) {
if (block[i][j] != EMPTY) {
if (y + i >= ROWS || x + j < 0 || x + j >= COLS || map[y + i][x + j] != EMPTY) {
return 0;
}
}
}
}
return 1;
}
void move_down()
{
erase_block(block_x * 2, block_y);
if (is_valid(block_x, block_y + 1)) {
block_y++;
draw_block(block_x * 2, block_y);
} else {
int i, j;
for (i = 0; i < BLOCK_SIZE; i++) {
for (j = 0; j < BLOCK_SIZE; j++) {
if (block[i][j] != EMPTY) {
map[block_y + i][block_x + j] = BLOCK;
}
}
}
int full_rows = 0;
for (i = 0; i < ROWS; i++) {
int j, full = 1;
for (j = 0; j < COLS; j++) {
if (map[i][j] == EMPTY) {
full = 0;
break;
}
}
if (full) {
full_rows++;
int k;
for (k = i; k > 0; k--) {
for (j = 0; j < COLS; j++) {
map[k][j] = map[k - 1][j];
}
}
for (j = 0; j < COLS; j++) {
map[0][j] = EMPTY;
}
}
}
if (full_rows > 0) {
mvprintw(ROWS + 1, 0, "Score: %d", full_rows * 10);
refresh();
timeout(2000);
timeout(1000);
}
new_block();
}
}
void move_left()
{
erase_block(block_x * 2, block_y);
if (is_valid(block_x - 1, block_y)) {
block_x--;
draw_block(block_x * 2, block_y);
} else {
draw_block(block_x * 2, block_y);
}
}
void move_right()
{
erase_block(block_x * 2, block_y);
if (is_valid(block_x + 1, block_y)) {
block_x++;
draw_block(block_x * 2, block_y);
} else {
draw_block(block_x * 2, block_y);
}
}
void rotate()
{
int i, j, temp[BLOCK_SIZE][BLOCK_SIZE];
for (i = 0; i < BLOCK_SIZE; i++) {
for (j = 0; j < BLOCK_SIZE; j++) {
temp[i][j] = block[BLOCK_SIZE - j - 1][i];
}
}
if (is_valid(block_x, block_y) && is_valid(block_x, block_y + 1)) {
for (i = 0; i < BLOCK_SIZE; i++) {
for (j = 0; j < BLOCK_SIZE; j++) {
block[i][j] = temp[i][j];
}
}
erase_block(block_x * 2, block_y);
draw_block(block_x * 2, block_y);
}
}
void game_loop()
{
new_block();
while (1) {
int c = getch();
switch (c) {
case 'q':
endwin();
exit(0);
break;
case KEY_LEFT:
move_left();
break;
case KEY_RIGHT:
move_right();
break;
case KEY_UP:
rotate();
break;
case KEY_DOWN:
move_down();
break;
default:
break;
}
}
}
int main()
{
init();
game_loop();
endwin();
return 0;
}
```
这个代码实现了一个简单的俄罗斯方块游戏,使用了ncurses库来实现游戏的界面和用户输入响应,使用了随机数来随机生成方块,并且实现了方块的移动和旋转等操作。您可以将代码保存为一个C源文件,然后使用gcc命令编译即可。
```
gcc -o tetris tetris.c -lncurses
```
运行游戏的命令为:
```
./tetris
```
希望这个简单的代码示例能够帮助到您,如果您有任何问题或者需要更多的帮助,请随时联系我。
使用C语言写能在linux环境下运行
下面是一个在Linux环境下运行的生产者消费者问题的代码,使用C语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define BUF_SIZE 10
int buffer[BUF_SIZE]; // 缓冲区
int in = 0; // 生产者放置产品的位置
int out = 0; // 消费者取产品的位置
sem_t empty; // 信号量,表示缓冲区中空余的数量
sem_t full; // 信号量,表示缓冲区中已有的数量
pthread_mutex_t mutex; // 互斥锁,保证同一时刻只有一个线程访问缓冲区
// 生产者线程函数
void *producer(void *arg) {
int i, item;
for (i = 0; i < 20; i++) {
item = rand(); // 随机生成一个产品
sem_wait(&empty); // 等待缓冲区空余
pthread_mutex_lock(&mutex); // 加锁
buffer[in] = item; // 放置产品
printf("Producer produces: %d\n", item);
in = (in + 1) % BUF_SIZE; // 指向下一个位置
pthread_mutex_unlock(&mutex); // 解锁
sem_post(&full); // 增加已有的数量
sleep(1); // 休眠一秒
}
pthread_exit(NULL);
}
// 消费者线程函数
void *consumer(void *arg) {
int i, item;
for (i = 0; i < 20; i++) {
sem_wait(&full); // 等待缓冲区有产品
pthread_mutex_lock(&mutex); // 加锁
item = buffer[out]; // 取出产品
printf("Consumer consumes: %d\n", item);
out = (out + 1) % BUF_SIZE; // 指向下一个位置
pthread_mutex_unlock(&mutex); // 解锁
sem_post(&empty); // 增加空余的数量
sleep(2); // 休眠两秒
}
pthread_exit(NULL);
}
int main(int argc, char *argv[]) {
pthread_t tid1, tid2;
// 初始化信号量和互斥锁
sem_init(&empty, 0, BUF_SIZE);
sem_init(&full, 0, 0);
pthread_mutex_init(&mutex, NULL);
// 创建生产者和消费者线程
pthread_create(&tid1, NULL, producer, NULL);
pthread_create(&tid2, NULL, consumer, NULL);
// 等待线程结束
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
// 销毁信号量和互斥锁
sem_destroy(&empty);
sem_destroy(&full);
pthread_mutex_destroy(&mutex);
return 0;
}
```
在该代码中,使用了pthread库中的线程相关函数和semaphore.h头文件中的信号量相关函数,还使用了互斥锁来保证线程同步。生产者线程每隔1秒生产一个随机数放入缓冲区中,消费者线程每隔2秒从缓冲区中取出一个产品进行消费。运行该代码可以看到生产者和消费者线程交替执行,符合生产者消费者问题的解决方案。