[英]RNG crashing c++ program
I am currently coding a roguelike, and naturally am using a lot of random number generation. 我目前正在编码“流氓”游戏,并且自然会使用大量随机数生成器。
The problem I'm running up on is that if I "overheat" rand(); 我遇到的问题是,如果我“过热” rand(); my program will crash. 我的程序将崩溃。
If i'm only generating 20 or so ints per frame, it's fine... but when the amount of random numbers goes into the hundreds, the program crashes. 如果我每帧仅生成20个左右的整数,就可以了……但是,当随机数达到数百时,程序崩溃。 The more I'm producing every frame, the sooner it crashes... which leads me to believe there is some pileup going on. 我生产的每个帧越多,它崩溃的时间就越早……这使我相信会有一些堆积。
I've done tests, and at 20 rand(); 我已经做过测试,并且是20 rand(); calls per frame, it will run for 24 hours straight at max speed without crashing. 每帧调用一次,它将以最大速度连续运行24小时而不会崩溃。 Triple that and it doesn't make it ten minutes. 将其增加三倍,它不会十分钟。
If I put srand(); 如果我放srand(); in the initialization, i can churn out thousands of random numbers before it locks up - but if I put srand(); 在初始化中,我可以在锁定之前删除成千上万的随机数-但是如果我放srand(); within the frame itself, i make it about 2-8 frames. 在框架本身内,我使其大约2-8帧。 If it matters, I'm using time(null) to seed. 如果重要的话,我正在使用时间(空)作为种子。
the more frequently i call rand(); 我调用rand()的频率越高; the sooner it crashes. 它越早崩溃。
Help? 救命?
The function rand() is not reentrant or thread-safe, since it uses hidden state that is modified on each call. 函数rand()不可重入也不是线程安全的,因为它使用在每次调用时都会修改的隐藏状态。 This might just be the seed value to be used by the next call, or it might be something more elaborate. 这可能只是下一个调用要使用的种子值,或者可能更复杂。 In order to get reproducible behavior in a threaded application, this state must be made explicit. 为了在线程化应用程序中获得可重现的行为,必须使此状态明确。 The function rand_r() is supplied with a pointer to an unsigned int, to be used as state. 函数rand_r()提供了一个指向无符号int的指针,该指针将用作状态。 This is a very small amount of state, so this function will be a weak pseudo-random generator. 这是非常少量的状态,因此此函数将是一个弱伪伪生成器。 Try drand48_r(3) instead. 尝试改用drand48_r(3)。
Try running it under a debugger 尝试在调试器下运行它
$ gdb myprog
(gdb) break main
(gdb) run
(gdb) record
eg 例如
(gdb) break abort
(gdb) break exit
since it is c++: 由于它是c ++:
(gdb) catch throw
(gdb) catch exception
and finally (gdb) continue 最后(gdb)继续
When it stops, reverse-continue until you find the culprit 停止时,反向继续直到找到罪魁祸首
Option 2: 选项2:
valgrind --tool=massif --massif-out-file="massif.out.%p" myprog
ms_print massif.out.*
to examine heap profiling. 检查堆分析。 Not unlikely you have a memory leak 不太可能出现内存泄漏
It's possible the high number of calls to rand are coming up with a number in a relatively small range which your code cannot handle. 可能有大量的rand调用会导致代码无法处理的相对较小的数字。 Try replacing your calls to rand with a function that just increments a number and returns it, and see if it eventually fails. 尝试用仅增加数字并返回数字的函数替换对rand的调用,然后查看它是否最终失败。
A few comments and ideas on how to narrow down the source of the issue: 关于如何缩小问题来源的一些意见和想法:
srand()
or rand()
functions causing the crash/lock up. 几乎可以肯定不是导致崩溃/锁定的srand()
或rand()
函数。 Chances are that one, or more, combinations of random numbers is getting your engine into a state where something bad happens. 一个或多个随机数的组合可能会使您的引擎进入发生不良情况的状态。 srand(NULL)
try using a constant seed like srand(12345)
. 而不是使用srand(NULL)
尝试使用像srand(12345)
这样的常量种子。 Depending on what other factors your engine uses (like user input) this may be enough to get it to crash in the same spot each time. 根据引擎使用的其他因素(例如用户输入),这足以使它每次都崩溃在同一位置。 #ifdef
s, commenting out code, setting application options, or even creating a temporary copy of the project so you can simply delete code, compile, and test. 这可能涉及#ifdef
,注释掉代码,设置应用程序选项甚至创建项目的临时副本,以便您可以简单地删除代码,编译和测试。 This may be difficult if the project is large/complex. 如果项目很大/很复杂,这可能很困难。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.