srand

How does srand relate to rand function?

坚强是说给别人听的谎言 提交于 2019-11-27 11:50:26
I understand that rand() function generates the same number(s) each you run it if you don't change the seed number. That's where srand() comes in. Time is always changing so I know that you should pass the time(null) parameter to srand. My question is with the code below from a tutorial site. int main() { int i, n=5; time_t t; /* Intializes random number generator */ srand((unsigned) time(&t)); /* Print 5 random numbers from 0 to 50 */ for( i = 0 ; i < n ; i++ ) { printf("%d\n", rand() % 50); } return(0); } I see no link from the srand ((unsigned) time(&t)); and rand. printf("%d\n", rand() %

If I don't specify srand(), what seed does rand() use?

穿精又带淫゛_ 提交于 2019-11-27 07:49:24
问题 Here is the code: #include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { int r; int i; for (i = 0; i < 100; i++) { r = rand() % 100 + 1; printf("%d\n", r); } return 0; } I've been trying to random number but one day, I forgot to put srand() in, but the rand() function can still random a number (the same sequence). The question is, what seed does it use if I don't specify it? 回答1: If srand is not called, rand acts as if srand(1) has been called. http://www.acm.uiuc.edu

随机数

梦想与她 提交于 2019-11-27 05:50:39
定义和用法 srand() 函数播种随机数生成器(rand())。 提示 :从 PHP 4.2.0 开始,随机数生成器自动播种,因此没有必要使用该函数 srand(seed)。 seed可选 时间函数time() :Return the time as seconds elapsed since midnight, January 1, 1970, or -1 in the case of an error. time(NULL),因为时间每秒都在变化,因此srand()里面的参数也时刻在变化,可以达到随机的要求!!! #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand((unsigned int)time(NULL)); printf("%d\n",rand() %10); // 0-9 printf("%d\n",rand() %11 +10); // 10 - 19 printf("%d\n",rand()); printf("%d\n",rand()); printf("%d\n",rand()); /*printf("%d\n",time(NULL));*/ return 0; } 来源: https://blog.csdn.net/weixin_44656803

Problems when calling srand(time(NULL)) inside rollDice function

﹥>﹥吖頭↗ 提交于 2019-11-27 05:13:04
When I used at first srand(time(NULL)) in rollDice() function it did not work. But when I put it in main, it works. Why is that? Can you tell me the logic? #include <stdio.h> #include <time.h> #include <stdlib.h> int rollDice(void) { return (1+rand()%6) + (1+rand()%6); } int main(void) { int roll; srand(time(NULL)); roll = rollDice(); printf("You rolled %d.\n", roll); enum Gamestatus {WON,LOST,CONTINUE}; enum Gamestatus status; while(status==CONTINUE){ printf("You are rolling again: \n"); printf("You rolled %d\n", roll = rollDice()); if (targetPoint==roll){ printf("You win!"); status=WON; }

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

让人想犯罪 __ 提交于 2019-11-27 04:07:39
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;i++) Arr[i] = rand()%size; First call (random): 6 0 2 0 6 7 5 5 8 6 Second call (random but same as

srand function is returning same values

牧云@^-^@ 提交于 2019-11-26 23:12:52
Hey guys take a look at this program. /* The craps game, KN king page 218 */ #include <stdio.h> #include <time.h> #include <stdbool.h> #include <stdlib.h> int roll_dice(void); bool play_game(void); int roll_dice(void) { int roll; getchar(); srand((unsigned) time(NULL)); roll = rand() % 13; if(roll == 0) roll = roll + 1; return roll; } bool play_game() { int sum = 0, wins = 0, loss = 0, point; sum = roll_dice(); printf("You rolled: %d", sum); if(sum == 7 || sum == 11) { printf("\nYou won!\n"); return true; } if(sum == 2 || sum == 3 || sum == 12) { printf("\nYou lost!!"); return false; } point =

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

本秂侑毒 提交于 2019-11-26 23:10:43
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_size)+min; while (randx <= min && randx >= max); x = randx; do randy = (rand()/bucket_size)+min; while

How does srand relate to rand function?

点点圈 提交于 2019-11-26 22:21:15
问题 I understand that rand() function generates the same number(s) each you run it if you don't change the seed number. That's where srand() comes in. Time is always changing so I know that you should pass the time(null) parameter to srand. My question is with the code below from a tutorial site. int main() { int i, n=5; time_t t; /* Intializes random number generator */ srand((unsigned) time(&t)); /* Print 5 random numbers from 0 to 50 */ for( i = 0 ; i < n ; i++ ) { printf("%d\n", rand() % 50);

Random numbers in C

╄→尐↘猪︶ㄣ 提交于 2019-11-26 22:00:55
问题 for(i = 0; i < n; i++){ srand(time(NULL)); printf("%d ", time(NULL)); for(j = 0; j < (n-1); j++){ a[i][j] = rand(); } } I try to generate random numbers, but they are the same... I try srand(i * time(NULL)) . No matter.. What should i do? Array declaration: int** a; int i; printf("Enter array size: "); scanf("%d", &n); a = (int**)calloc(n, sizeof(int)); for(i = 0; i < n; i++) a[i] = (int*)calloc(n-1, sizeof(int)); 回答1: Call srand() outside of the loop. You are reseeding it every iteration.

How does calling srand more than once affect the quality of randomness?

别来无恙 提交于 2019-11-26 21:51:14
问题 This comment, which states: srand(time(0)); I would put this line as the first line in main() instead if calling it multiple times ( which will actually lead to less random numbers ). ...and I've bolded the line which I'm having an issue with... repeats common advice to call srand once in a program. Questions like srand() — why call only once? re-iterate that because time(0) returns the current time in seconds, that multiple calls to srand within the same second will produce the same seed. A