C程序中的随机数生成器未生成正确的随机数

时间:2020-02-23 21:11:21

标签: c function pointers random struct

我正在从事一个有关游戏的项目,该项目涉及创建带有怪物,奖品或空的随机生成的房间。但是,每次我运行程序时,它都会将房间设置为奖品,甚至会为奖品给您的积分数量生成错误的数字。

#include <stdio.h>
#include <stdlib.h>
#include "monsterbattle.h"
#include <time.h>

int main(int argc, char *argv[]) {

Character user;
int numRooms;

srand(time(NULL));

user.hitPoints = 50;
user.experiencePoints = 0;

printf("Welcome to Monster-Battle!\n");
printf("Enter the amount of rooms you would like to endure: ");
scanf("%d", &numRooms);

printf("You have decided to endure %d rooms, good luck!\n", numRooms);
printf("----------------\n");
printCharacter(user);

struct Room *roomArray = malloc(sizeof(struct Room) * numRooms);

fillRooms(roomArray, numRooms);

enterRoom(roomArray, user, numRooms);

free(roomArray);

return 0;

}

这是我的主要功能,我在其中调用所有功能。

#include <stdio.h>
#include <stdlib.h>
#include "monsterbattle.h"
#include <time.h>

void printCharacter(Character c) {

printf("Player Stats: (HP: %d XP: %d)\n", c.hitPoints, c.experiencePoints);

}

int getRandomNumber(int min, int max) {

return min + rand() % (max+1 - min);

}

void fillRooms(Room *rooms, int length) {

for (int i = 0; i < length; i++) {

int randomNum = getRandomNumber(0, 9);

if (randomNum == 0) {
rooms[i].type = EMPTY;
}

else if ((randomNum >= 1) && (randomNum <= 4)) {
rooms[i].type = PRIZE;
rooms[i].prize.points = getRandomNumber(5, 10);
}

else if ((randomNum >= 5) && (randomNum <= 9)) {
rooms[i].type = MONSTER;
rooms[i].monster.hitPoints = getRandomNumber(10, 30);

if (rooms[i].monster.hitPoints % 3 == 0) {
rooms[i].monster.experiencePoints = 1;
}
else if (rooms[i].monster.hitPoints % 3 != 0) {
rooms[i].monster.experiencePoints = 0;
}

}

}

}

void enterRoom(Room *room, Character player, int length) {

for (int i = 0; i < length; i++) {

printf("----------------\n");
printf("ROOM %d OF %d...\n", i+1, length);

if (room[i].type = EMPTY) {
printf("This room is empty.\n");
printCharacter(player);
}
if (room[i].type = EMPTY) {
printf("This room is empty.\n");
printCharacter(player);
}

else if (room[i].type = PRIZE) {
printf("This room has a prize. (PTS: %d)\n", room[i].prize.points);
player.hitPoints = player.hitPoints + room[i].prize.points;
printf("You gained %d hit points!\n", room[i].prize.points);
printCharacter(player);
}

else if (room[i].type = MONSTER) {
printf("This room has a monster. (HP: %d XP: %d)\n", room[i].monster.hitPoints, room[i].monster.experiencePoints);
player.hitPoints = player.hitPoints - room[i].monster.hitPoints;
player.experiencePoints = player.experiencePoints + room[i].monster.experiencePoints;
printf("You lost %d hit points!\n", room[i].monster.hitPoints);

if(room[i].monster.experiencePoints = 1) {
printf("You gained %d experience point!\n", room[i].monster.experiencePoints);
}

if(player.hitPoints <= 0) {
printf("Sorry, you died! Better luck next time...\n");
}

printCharacter(player);
}

}

if(player.hitPoints > 0) {
printf("----------------\n");
printf("Congratulations! You survived the game!\n");
}

}

,这里是我定义所有函数的地方。任何帮助将不胜感激!

int getRandomNumber(int min, int max) {

return min + rand() % (max+1 - min);

}

这是我遇到的功能。我的教授要求我们为srand()设置种子,这是我在主要功能中所做的,但是生成的唯一空间是PRIZE,甚至甚至没有达到预期的行为。

1 个答案:

答案 0 :(得分:4)

您比较错了。

{
  "id": 123,
  "shops": [
    {
      "shopId": 456,
      "products": [
        {
          "productId": 10001,
          "name": "abc",
          "state": "active"
        }
      ]
    },
    {
      "shopId": 789,
      "products": [
        {
          "productId": 20002,
          "name": "jbf",
          "state": "active"
        }
      ]
    }
  ]
}

一个体面的编译器在发现一个比较比较平常的作业时会警告您,因此打开并注意编译器警告

再次:打开并注意编译器警告

相关问题