C 如何让我的随机函数工作?
随机函数在参数集中不起作用,我不知道为什么。有人能帮忙吗?我需要18到38之间的随机数,但我似乎无法得到,我不知道为什么 这是我的密码C 如何让我的随机函数工作?,c,random,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 tire
#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);
}
}
#包括
#包括
#包括
结构轮胎
{
煤焦制造商[40];
轮胎内部压力[2];
内压变化;
}轮胎;
无效信息(轮胎*,整数);
无效轮胎压力(轮胎*,内部);
int main()
{
轮胎[4];
轮胎*ptire=&轮胎[0];
获取信息(ptire,4);
轮胎压力(ptire,4);
返回0;
}
void gettire信息(轮胎*ptire,整数大小)
{
int i=0;
对于(i=0;i制造商;
}
printf(“您输入的所有轮胎品牌…仅用于验证:\n”);
对于(i=0;i制造商);
}
无效轮胎压力(轮胎*ptire,内部尺寸)
{
int i=0;
int min=18;
int max=38;
对于(i=0;i胎压);
}
}
编辑:这是我在建议修复后更新的功能
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);
}
}
void轮胎压力(轮胎*ptire,内部尺寸)
{
int i=0;
int min=18;
int max=38;
对于(i=0;i胎压);
}
}
不需要调用srand(time(NULL))代码>每次生成随机数时。在任何函数调用之前,将其放入main()
然后改变
rand() % (max - min) - min;
到
假设max
=3和min
=1,则需要rand()%3+1
生成一个从1到3的随机数
还有另一个与随机数生成无关的问题:生成的随机数被分配给ptire
,也就是说,您分配的轮胎*
带有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]);
}
}
现在所有的数字都在18到38之间。请注意,tire\u pressure
是一个包含两个int
s的数组。在不知道您的目的的情况下,我只是给了它的第一个元素随机数。srand
应该调用一次,而且只能调用一次。将该行放在main
的开头。另外,您需要的是+min
而不是-min
。当您应该分配给ptire[i]时,您正在将结果分配给ptire
。胎压
,和……。而且(max-min)
应该是(max-min+1)
。所以你应该有rand()%(max-min+1)+min代码>这将为您提供min
的完整范围<代码>最大值
,包括在内。如前所述,您应该只调用srand
一次,可能是从main
调用。我已经完成了这些操作,但它仍然不在18到38之间。这是更新的代码,但仍然没有任何内容。我还用ptire[I]轮胎压力试过,但还是不起作用。我得到的是随机数,而不是我需要的,它们要么太大,要么太小little@Charlie我的荣幸:)如果您认为我的答案对您有帮助,您可以单击左侧分数下方的灰色勾号接受它。
#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