srand

How often should I call srand() in a C++ application?

大城市里の小女人 提交于 2019-11-26 17:23:23
问题 I have a C++ application which calls rand() in various places. Do I need to initialize srand() regularly to ensure that rand() is reasonably random, or is it enough to call it once when the app starts? 回答1: If you have only a single thread, seed once . If you reseed often, you might actually break some of the statistical properties of the random numbers. If you have multiple threads, don't use rand at all, but rather something threadsafe like drand48_r , which lets you maintain a per-thread

Rand() % 14 only generates the values 6 or 13

北战南征 提交于 2019-11-26 16:40:48
Whenever I run the following program the returned values are always 6 or 13. #include <iostream> #include <fstream> #include <ctime> #include <cstdlib> using namespace std; //void randomLegs(); //void randomPush(); //void randomPull(); //void randomMisc(); int main(int argc, const char * argv[]) { srand(time(NULL)); //randomLegs(); cout << rand() % 14; return 0; } I have run the program close to a hundred times during today and yesterday. Can anyone tell me what I'm doing wrong? Thank you. EDIT: By the way, if I change the range of rand() to say 13 or 15 it works just fine. I can reproduce the

C program - srand() [duplicate]

我与影子孤独终老i 提交于 2019-11-26 11:40:58
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Recommended way to initialize srand? I have the following problem when using srand() in c. I call srand(time(NULL)) in a loop , but the loop finishes before 1 sec and every time I call the rand() I get the same value. How can I solve this? 回答1: because the seed is bound into the time() which are seconds since unix epoch, basically you're giving it the same seed because the loop takes less than a second. What you

How to use function srand() with time.h? [duplicate]

纵饮孤独 提交于 2019-11-26 11:03:49
问题 This question already has an answer here: srand() — why call it only once? 7 answers My program contains code that should generate a random positive integer number every time I execute it. It generates random numbers but only once. After that, when I execute same code, it gives me same values, and it is making my code useless. I started with the rand function, and then I used the srand() function with the time.h header file, but still it is not working properly. #define size 10 for(i=0;i<size

rand() generating the same number – even with srand(time(NULL)) in my main!

ⅰ亾dé卋堺 提交于 2019-11-26 08:34:49
问题 So, I\'m trying to create a random vector (think geometry, not an expandable array), and every time I call my random vector function I get the same x value, though y and z are different. int main () { srand ( (unsigned)time(NULL)); Vector<double> a; a.randvec(); cout << a << endl; return 0; } using the function //random Vector template <class T> void Vector<T>::randvec() { const int min=-10, max=10; int randx, randy, randz; const int bucket_size = RAND_MAX/(max-min); do randx = (rand()/bucket

srand() — why call it only once?

余生颓废 提交于 2019-11-25 21:36:35
问题 This question is about a comment in this question Recommended way to initialize srand? The first comment says that srand() should be called only ONCE in an application. Why is it so? 回答1: That depends on what you are trying to achieve. Randomization is performed as a function that has a starting value, namely the seed . So, for the same seed, you will always get the same sequence of values. If you try to set the seed every time you need a random value, and the seed is the same number, you