每2秒生成一个随机数? C ++ Srand [英] Generate a Random number every 2 seconds? C++ Srand

查看:553
本文介绍了每2秒生成一个随机数? C ++ Srand的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C ++,为了做一个游戏,我能够使用srand的函数每秒生成一个随机数。但我想要的数字每2秒不同。

解决方案



这里是一个简单的方法来修复代码。



在无限中放置 clock(),而循环,并让时钟计数,以便当它达到两秒,它触发 rand()生成一个新的随机数。重置 clock()



如你所知,delta时间是最后的时间,减去原始时间。

  dt = t  -  t0 

这个增量时间只是在 while 循环中经过的时间量。



a 函数的导数表示函数关于其中一个变量的无穷小变化。 deltaTime



函数关于变量的导数定义为 http://mathworld.wolfram.com/Derivative.html

  f(x + h)-f(x)
f'(x)= lim -----------------
h-> 0 h

首先你得到一个时间, em> TimeZero = clock(),以供参考。



得到并通过 h 来实现。 h CLOCKS_PER_SEC 。现在增量时间是

  deltaTime =(clock() -  TimeZero)/ CLOCKS_PER_SEC; 

deltaTime>



将所有代码转换为代码结果:

  #include< cstdlib> 
#include< ctime>
#include< iostream>
using namespace std;

int main(int argc,char * argv []){
cout< 每2秒生成一个新的随机数\\\
\\\
;

//创建时钟和启动计时器
clock_t TimeZero = clock(); //启动计时器

double deltaTime = 0;
double secondsToDelay = 2;
bool exit = false;

//使用时间生成随机种子
srand(time(0));

while(!exit){

//获取以秒为单位的增量时间
deltaTime =(clock() - TimeZero)/ CLOCKS_PER_SEC;
cout<< \b<< secondsToDelay-deltaTime< \b;

//比较delta时间是否为2秒或以上
if(deltaTime> secondsToDelay){
cout< ;

//生成新的随机数
int i = rand()%100 + 1;
cout<< \\\
New random:<< i<< \\\
;

//复位时钟定时器
deltaTime = clock();
TimeZero = clock()
}
}

return 0;
}


I am studying C++ in order to make a game and I was able to generate a random number every second using the functions of srand. But I wanted the number to be different every 2 second instead.

解决方案

Here is a simple way to fix the code.

Put a clock() in an infinite while loop and let the clock count so that when it reaches two seconds, it triggers rand() to generate a new random number. Reset the clock(). Repeat infinitely.

Now the Math behind:

As you already know, delta time is the final time, minus the original time.

dt = t - t0

This delta time, though, is simply the amount of time that passes while in the while loop.

The derivative of a function represents an infinitesimal change in the function with respect to one of its variables. Our deltaTime.

The derivative of a function with respect to the variable is defined as http://mathworld.wolfram.com/Derivative.html

                f(x + h) - f(x)
f'(x) = lim    -----------------
        h->0           h

First you get a time, i.e TimeZero = clock() , for reference.

Then you subtract that time from a new time you just got and devide it by h. h is CLOCKS_PER_SEC. Now delta time is

deltaTime = (clock() - TimeZero) / CLOCKS_PER_SEC;

And when deltaTime > secondsToDelay, you generate a new random number.

Putting all that into code results in this:

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

int main(int argc, char *argv[]){
    cout << "Generate a new random number every 2 seconds \n\n";

    // create a clock and start timer
    clock_t TimeZero = clock(); //Start timer

    double deltaTime = 0;
    double secondsToDelay = 2;
    bool exit = false;

    // generate random seed using time 
    srand(time(0)); 

    while(!exit) {

        // get delta time in seconds
        deltaTime = (clock() - TimeZero) / CLOCKS_PER_SEC;
        cout << "\b" << secondsToDelay - deltaTime << "\b";

         // compare if delta time is 2 or more seconds
        if(deltaTime > secondsToDelay){
            cout << "                      ";

            // generate new random number
            int i = rand() % 100 + 1; 
            cout << "\nNew random : " << i << "           \n"; 

            //reset the clock timers
            deltaTime = clock();
            TimeZero = clock();
        }
      }

    return 0;
}

这篇关于每2秒生成一个随机数? C ++ Srand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆