在一定的时间范围内随机做一些事情 [英] Do something randomly within a certain time frame

查看:37
本文介绍了在一定的时间范围内随机做一些事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个间隔为 1000 毫秒的计时器.我想在随机时间但在给定的时间范围内做某事.例如.如果我将时间范围指定为 10 秒、1-10、11-20、21-30 等.这意味着在前 10 秒内,在第一个时间帧 1 到 10 秒之间会随机发生一些事情.例如.可能发生在第 2 秒或第 8 秒.然后再次从 11 到 20 秒,在第二个时间范围之间的任何时间再次发生某些事情.例如.这可能是第 13 或第 15 秒.请注意,计时器必须是连续的,例如从 1 到 100 秒.在这里,如果我将时间范围指定为 10,我们将有十个时间范围.1-10,11-20,21-30 ....91-100.因为我需要知道将发生的事件的确切时间.因此不要认为重置定时器就可以解决问题.

I have a timer with interval of 1000 ms. I want to do something at random time but within a given time frame. E.g. If I specify the time frame to be for example 10 seconds, 1-10, 11-20, 21-30 and so on. This means that in the first 10 seconds something will happen between the 1st time frame 1 to 10 seconds at random. E.g. could happen in the 2nd second or the 8th second. Then again from 11 to 20 seconds something occurs again any time between the second time frame. E.g. this could be the 13th or the 15th second. Note that the timer has to be continuous e.g. from 1 to 100 seconds. Here if I specifiy the time frame to be 10 we will have ten time frames. 1-10,11-20,21-30 ....91-100. Since I need to know the exact time of the events that will occur. Therefore do not think that resetting the timer might solve the problem.

感谢您的回复,非常感谢您的解释示例.

Thanks for you reply and examples with explanation are greatly appreciated.

推荐答案

我认为前 3 个答案中的任何一个都不正确,所以让我尝试一下:您说的是时间在流逝,并且在每个N 秒的切片,您希望在该切片中的随机时间发生一个事件.如果是这样,那么每当时间是 N 秒的倍数时,获取 0 到 N 之间的随机数,并在该时间生成事件.例如,在伪代码中:

I don't think any of the first 3 answers have it right, so let me try: what you are saying is that time is ticking, and in each slice of N seconds, you want one event to occur at a random time in that slice. If that is the case, then whenever the time is a multiple of N seconds, get a random number between 0 and N, and generate the event at that time. For example, in pseudo-code:

time = 0
delta_time = 1 second
max_time = 100 seconds
slice_time = 10 seconds
// decide when event of first slice will occur; can't be at 0: get random # 
// in range [delta_time, slice_time]:
next_rand_time = random(delta_time, slice_time)
setup timer to fire at interval of delta_time, each time do the following (callback): 
    time += delta_time
    // is it time to fire the event yet? allow for round-off error:
    if abs(time - next_rand_time) < 0.0001:
        generate event
    // is it time to decide when next random event will be? 
    if modulo(time, slice_time) == 0: 
        // can't be on left boundary; get random # in range [delta_time, slice_time]:
        next_rand_time = time + random(delta_time, slice_time)

这 5 个变量(时间、delta_time 等)很可能是使用计时器的类的数据成员,而回调将是该类的一个方法.在您的帖子中提供一些代码以获取更多详细信息.

The 5 variables (time, delta_time, etc) are likely going to be data members of the class that uses the timer, and the callback will be a method of the class. Provide some code in your post for further details.

这篇关于在一定的时间范围内随机做一些事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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