c - 如何让我的随机函数工作?

标签 c random

随机函数在参数集中不起作用,我不知道为什么。谁能帮忙?我需要 18 到 38 之间的随机数,但我似乎无法得到它,我也不知道为什么。

这是我的代码

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

struct tires
{
    char Manufacturer[40];
    int tire_pressure[2];
    int pressure_change;
}typedef tires;

void getTireInformation(tires*, int);
void tirePressure(tires*, int);
int main()
{
    tires tire[4];
    tires* ptire = &tire[0];

    getTireInformation(ptire, 4);
    tirePressure(ptire, 4);

    return 0;
}

void getTireInformation(tires* ptire, int size)
{
    int i = 0;
    for (i = 0; i < size; i++)
    {
        printf("please enter Make for the tire: \n");
        scanf("%s", &(ptire + i) ->Manufacturer);
    }

    printf("all tire make you entered ...just for verification:\n");
    for(i = 0; i < size; i++)
        printf("%s\n",(ptire +i) ->Manufacturer);
}

void tirePressure(tires* ptire, int size)
{
    int i = 0;
    int min = 18;
    int max = 38;
    for (i = 0; i < size; i++)
    {
        srand(time(NULL));
        ptire = rand()%(max - min)-min;
        printf("%d\n", (ptire + i) -> tire_pressure);
    }
}

编辑:这是我在进行建议修复后更新的函数

void tirePressure(tires* ptire, int size)
{
    int i = 0;
    int min = 18;
    int max = 38;
    for (i = 0; i < size; i++)
    {
        ptire = rand()%(max - min + 1) + min;
        printf("%d\n", (ptire + i) -> tire_pressure);
    }
}

最佳答案

不必每次生成随机数时都调用srand(time(NULL));。将它放在 main() 中,在任何函数调用之前。

然后改变

rand() % (max - min) - min;

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

假设 max = 3 和 min = 1,你需要 rand() % 3 + 1 来生成一个从 1 到3 包括在内。

还有一个与随机数生成无关的问题:生成的随机数分配给ptire,即你分配的是一个tires*int!


我已经完善了您的代码。希望它会起作用:

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

struct tires
{
    char Manufacturer[40];
    int tire_pressure[2];
    int pressure_change;
} typedef tires;

// Prototypes
void getTireInformation(tires*, size_t);
void tirePressure(tires*, size_t);

int main()
{
    tires tire[4];
    tires* ptire = &tire[0];

    srand(time(NULL));
    getTireInformation(ptire, 4);
    tirePressure(ptire, 4);

    return 0;
}

void getTireInformation(tires* ptire, size_t size)
{
    size_t i = 0;
    for (i = 0; i < size; i++)
    {
        printf("Please enter the maker of the tire: \n");
        scanf("%s", (ptire + i) -> Manufacturer); // just use str. &str actually causes undefined bahavior
    }

    printf("All tire make you entered ...just for verification:\n");
    for(i = 0; i < size; i++)
        printf("%s\n", (ptire +i) -> Manufacturer);
}

void tirePressure(tires* ptire, size_t size)
{
    int i = 0;
    int min = 18;
    int max = 38;
    for (i = 0; i < size; i++)
    {
        (ptire + i) ->tire_pressure[0] = rand() % (max - min + 1) + min;
        printf("%d\n", (ptire + i) -> tire_pressure[0]);
    }
}

这是我运行时的结果:

Please enter the maker of the tire: 
qwert
Please enter the maker of the tire: 
fewqwe
Please enter the maker of the tire: 
hcgexf
Please enter the maker of the tire: 
zrbghcr
All tire make you entered ...just for verification:
qwert
fewqwe
hcgexf
zrbghcr
22
34
31
31

现在所有数字都在 18 到 38 之间。请注意,tire_pressure 是一个包含两个 int 的数组。不知道你的目的,我只是给它的第一个元素随机数。

关于c - 如何让我的随机函数工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36097614/

相关文章:

C指针void *缓冲区问题

c - 从c中的列表中读取函数中的结构

c - 如何为gcc编写自己的代码生成器后端?

arrays - 给定 4 个包含 1 到 10 个元素的数组。找到 3 个数字,其和可以生成所有 4 个数字?

php - 使用 PHP 生成随机唯一 ID

c++ - 如何使随机数(50 毫秒以下)不会重复两次

c - 被动套接字不接受 ftp

c代码在dumpbin中报undef sprintf 编译没有错误

performance - 有创意的数字猜谜游戏

JavaScript 不会从特定数组列表中随机选择名称