如何多次调用一个函数并在每次调用时更改函数内的随机数?

发布于 2024-11-01 20:07:29 字数 1693 浏览 1 评论 0原文

我正在为我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事还在继续 2024-11-08 20:07:29

不要自己调用 srand,删除这一行:

srand((unsigned)time(NULL)); //Uses time as seed for random numbers

您的 time() 调用只有一秒分辨率,每次循环运行所需的时间远少于一秒,结果是您每次都使用相同的值播种随机数生成器,并生成相同的一组随机数字。

现代 rand() 实现负责随机数生成器本身的播种。

大多数随机数生成器实际上是其种子的确定性函数,您的 rand() 看起来像是其中之一。 标准指的是rand()生成伪随机整数是有充分理由的。

Don't call srand yourself, delete this line:

srand((unsigned)time(NULL)); //Uses time as seed for random numbers

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 what rand() generates as pseudo-random integers for a good reason.

爱给你人给你 2024-11-08 20:07:29

一种选择是使用静态变量来指示您的种子是否已设置。此代码片段将展示如何执行此操作:

...
static int srand_flag = 0;
if (!srand_flag) {
  srand_flag = 1;
  srand((unsigned)time(NULL));
}
...

这将确保您只调用种子一次,因此不会遇到大多数 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:

...
static int srand_flag = 0;
if (!srand_flag) {
  srand_flag = 1;
  srand((unsigned)time(NULL));
}
...

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 like GetTickCount().

A final option, if you absolutely must call srand() each and every time, is to put a delay of one second before each call to srand() to ensure that the counter value is different on every call. (This is not a good option.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文