开发者

Best way to simulate a server which randomly sends alarms

Here's what I need to do:

I need to simulate a server, which sends alarms at random intervals, i.e. after 2 secs, then after 4, after 10 but always within some sort 开发者_开发技巧of range.

What is the best way to achieve this?

Here's my fist idea:

1.) Choose a random value between 0 and 10. 2.) Sleep for that amount of time 3.) Send an alarm to the client 4.) Repeat forever (i.e. while (true))


Your idea doesn't sound so bad. Of course, you could go a more sophisticated path using timers and signals, but it depends on the requirements and using simply sleep() is maybe the fastest and easiest way for your purposes. Go for it :)


For range 4-10 seconds, choose a random value 0..6 (10-4), add 4, sleep this amount of seconds (or get current time, add the value, and loop with a short sleep rechecking the time), send alarm, rinse, repeat.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜