如何多次调用一个函数并在每次调用时更改函数内的随机数?
我正在为我的 C 类制作一款游戏(实际上是重新制作一款),并且我有一个生成随机价格的函数。问题是我需要在整个游戏中调用此函数 60 次,并且每次调用该函数时都将数字重新生成为新的数字。是否可以在不结束程序的情况下执行此操作,如果可以,那么如何?
到目前为止,我已经为该函数编写了一个 for 循环。但它只是打印了 60 次相同的函数,这是我预料到的。
代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int Prices(void)
{
//Random price generator
int Acid = rand() % 10+ 1;
int Coke = rand() % 150+ 101;
int Crack = rand() % 30 + 15;
int Ecstasy = rand() % 8 + 1;
int Herion = rand() % 100 + 46;
int Meth = rand() % 100 + 26;
int Opium = rand() % 65 + 31;
int Pills = rand() % 7 + 1;
int Shrooms = rand() % 30 + 16;
int Speed = rand() % 45 + 11;
int Weed = rand() % 20 + 21;
//Prints the above random prices to the main screen
printf("PRICE PER UNIT:\n\n");
printf("Acid: ""%i\n",Acid);
printf("Coke: ""%i\n",Coke);
printf("Crack: ""%i\n",Crack);
printf("Ecstasy: ""%i\n",Ecstasy);
printf("Herion: ""%i\n",Herion);
printf("Meth: ""%i\n",Meth);
printf("Opium ""%i\n",Opium);
printf("Pills: ""%i\n", Pills);
printf("Shrooms: ""%i\n", Shrooms);
printf("Speed: ""%i\n",Speed);
printf("Weed: ""%i\n",Weed);
printf("********************************************************************************\n");
return Acid && Coke && Crack && Ecstasy && Herion && Meth && Opium && Pills && Shrooms && Speed && Weed;
}
int main(void){
int Prices(void);
int i;
for(i=0; i<60; i++){
Prices();
}
}
好吧,我删除了 srand 函数,它起作用了,但我还需要方法来限制该函数在整个游戏中仅定期调用 60 次,而不是一次全部调用。
I am making a game for my C class (actually remaking one) and I have a function that produces random prices. The problem is that I need to call this function 60 times throughout the game and have the numbers regenerate to new ones every time the function is called. Is it possible to do this without ending the the program, and if so then how?
So far I've written a for loop for the funct. but it just prints the same function 60 times which I kindof expected to happen.
Here is the code:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int Prices(void)
{
//Random price generator
int Acid = rand() % 10+ 1;
int Coke = rand() % 150+ 101;
int Crack = rand() % 30 + 15;
int Ecstasy = rand() % 8 + 1;
int Herion = rand() % 100 + 46;
int Meth = rand() % 100 + 26;
int Opium = rand() % 65 + 31;
int Pills = rand() % 7 + 1;
int Shrooms = rand() % 30 + 16;
int Speed = rand() % 45 + 11;
int Weed = rand() % 20 + 21;
//Prints the above random prices to the main screen
printf("PRICE PER UNIT:\n\n");
printf("Acid: ""%i\n",Acid);
printf("Coke: ""%i\n",Coke);
printf("Crack: ""%i\n",Crack);
printf("Ecstasy: ""%i\n",Ecstasy);
printf("Herion: ""%i\n",Herion);
printf("Meth: ""%i\n",Meth);
printf("Opium ""%i\n",Opium);
printf("Pills: ""%i\n", Pills);
printf("Shrooms: ""%i\n", Shrooms);
printf("Speed: ""%i\n",Speed);
printf("Weed: ""%i\n",Weed);
printf("********************************************************************************\n");
return Acid && Coke && Crack && Ecstasy && Herion && Meth && Opium && Pills && Shrooms && Speed && Weed;
}
int main(void){
int Prices(void);
int i;
for(i=0; i<60; i++){
Prices();
}
}
Ok So I deleted the srand function and that worked but I also need way to limit this function to being called only 60 times periodically throughout the game and not all at once.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要自己调用 srand,删除这一行:
您的 time() 调用只有一秒分辨率,每次循环运行所需的时间远少于一秒,结果是您每次都使用相同的值播种随机数生成器,并生成相同的一组随机数字。
现代
rand()
实现负责随机数生成器本身的播种。大多数随机数生成器实际上是其种子的确定性函数,您的
rand()
看起来像是其中之一。 标准指的是rand()
生成伪随机整数是有充分理由的。Don't call
srand
yourself, delete this line:Your
time()
call only has one second resolution and each run through the loop takes much much less than one second, the result is that you're seeding the random number generator with the same value every time and that yields the same set of random numbers.Modern
rand()
implementations take care of seeding the random number generator themselves.Most random number generators are actually deterministic functions of their seed, your
rand()
looks like it is one of these. The standard refers to whatrand()
generates as pseudo-random integers for a good reason.一种选择是使用静态变量来指示您的种子是否已设置。此代码片段将展示如何执行此操作:
这将确保您只调用种子一次,因此不会遇到大多数
time()
实现为您提供的 1 秒粒度。另一种选择是使用(通常特定于平台的)毫秒计时器来代替您的种子。然而,函数调用所提供的功能是高度依赖于平台的。在 Linux 中,您必须摆弄诸如
clock_gettime()
之类的东西,而在 Windows 中,您可能会摆弄诸如GetTickCount()
之类的东西。如果您绝对必须每次都调用
srand()
,最后一个选择是在每次调用srand()<之前延迟一秒/code> 以确保每次调用时计数器值都不同。 (这不是一个好的选择。)
One option is to use a static variable to signal if your seed has already been set. This snippet would show how to do it:
This will ensure that you only call your seed once and thus that you won't run into the 1-second granularity that most
time()
implementations give you.Another option is to use a (usually platform-specific) millisecond timer instead for your seed. What function call gives you that is highly platform-dependent, however. In Linux you'd have to mess around with things like
clock_gettime()
while in Windows you'd probably be messing around with things likeGetTickCount()
.A final option, if you absolutely must call
srand()
each and every time, is to put a delay of one second before each call tosrand()
to ensure that the counter value is different on every call. (This is not a good option.)