srand

Avoiding Repeated seed generation using srand()

被刻印的时光 ゝ 提交于 2019-12-02 09:49:59
I have a typical situation where I need to generate a batch of random numbers. I have used a loop which generates 100 random numbers on each pass: for(int i=0; i<npasses; i++) { srand(time(NULL)); //Initialize seed for(int j=0; j<100; j++) printf("%d ", rand()%10); printf("\n"); //New line after 100 numbers } Now, the inner loop executes in less than a millisecond. As a result, there is no change in the value of time(). This re-initializes the seed (srand()) to the same value and my random numbers are REPEATED.. Can anyone suggest a workaround/fix. Set the seed once, before the loop. Keep

srand()与rand()的应用

孤街浪徒 提交于 2019-12-02 03:17:51
1 #include<stdio.h> 2 #include<time.h> 3 #include<stdlib.h> 4 void bubbleSort(int arr[],int arrSize){ 5 int tmp = 0; 6 int swap = 1; 7 while(swap){ 8 swap = 0; 9 for(int i = 0;i < (arrSize -1);i++){ 10 if(arr[i] > arr[i + 1]){ 11 tmp = arr[i]; 12 arr[i] = arr[i + 1]; 13 arr[i + 1] = tmp; 14 swap = 1; 15 } 16 } 17 } 18 19 return; 20 } 21 22 void outArr(char* name, char* time,int arr[],int arrSize){ 23 printf("%s %s is: \n",name,time); 24 for(int i = 0;i < arrSize;i++){ 25 printf("%d\t",arr[i]); 26 } 27 printf("\n"); 28 return; 29 } 30 31 32 void insertSort(int arr[],int arrSize){ 33 for(int i =

rand() return the same number [duplicate]

最后都变了- 提交于 2019-12-01 23:26:21
This question already has an answer here: srand() — why call it only once? 7 answers I am making a simple example in C with rand() but the function always return the same number despite i am using srand(). This is the code: #include <stdio.h> #include <stdlib.h> #include <time.h> int generate(int min, int max) { srand(time(NULL)); return rand() % (max - min + 1) + min; } int main() { int i; for (i = 0; i < 10; i++) { printf("Number random %d = %d\n", i, generate(1, 100)); } return 0; } Execution : Number random 0 = 40 Number random 1 = 40 Number random 2 = 40 Number random 3 = 40 Number random

Is it good idea to pass uninitialized variable to srand?

送分小仙女□ 提交于 2019-12-01 22:07:33
Is it good idea to pass uninitialized variable to srand instead of result of time(NULL) ? It is one #include and one function call less. Example code: #include <stdlib.h> int main(void) { { usigned seed; //uninitialized srand(seed); } //other code return 0; } instead of #include <stdlib.h> #include <time.h> int main(void) { srand(time(NULL)); //other code return 0; } No, it isn't. Reading an uninitialized value results in undefined behavior. It can be zero, it can be semi-random — but as such, it can repeatedly be the same value. It may also cause compilation or runtime errors, or do any other

What are the weaknesses of Perl's srand() default seed, post version 5.004?

谁说我不能喝 提交于 2019-12-01 18:22:35
I can find plenty of documentation as to issues with use of time() prior to Perl version 5.004, but nothing following. For a homework assignment, we are asked to reverse-engineer a program's results based on the assumption that the default Perl srand() is still flawed in default seeding. The changelog for the perl 5.004 release states that the srand() default seed is now based on "a heavy mix of difficult-to-predict system-dependent values." Is that the case, and, if so, what are those values and do they have any inherent weaknesses? (I am not a cryptographer but I've absorbed a lot over the

Issues with seeding a pseudo-random number generator more than once?

江枫思渺然 提交于 2019-12-01 18:12:52
I've seen quite a few recommendations for not seeding pseudo-random number generators more than once per execution, but never accompanied by a thorough explanation. Of course, it is easy to see why the following (C/C++) example is not a good idea: int get_rand() { srand(time(NULL)); return rand(); } since calling get_rand several times per second produces repeated results. But wouldn't the following example still be an acceptable solution? MyRand.h #ifndef MY_RAND_H #define MY_RAND_H class MyRand { public: MyRand(); int get_rand() const; private: static unsigned int seed_base; }; #endif MyRand

Issues with seeding a pseudo-random number generator more than once?

一曲冷凌霜 提交于 2019-12-01 17:54:23
问题 I've seen quite a few recommendations for not seeding pseudo-random number generators more than once per execution, but never accompanied by a thorough explanation. Of course, it is easy to see why the following (C/C++) example is not a good idea: int get_rand() { srand(time(NULL)); return rand(); } since calling get_rand several times per second produces repeated results. But wouldn't the following example still be an acceptable solution? MyRand.h #ifndef MY_RAND_H #define MY_RAND_H class

What are the weaknesses of Perl's srand() default seed, post version 5.004?

我的未来我决定 提交于 2019-12-01 17:39:42
问题 I can find plenty of documentation as to issues with use of time() prior to Perl version 5.004, but nothing following. For a homework assignment, we are asked to reverse-engineer a program's results based on the assumption that the default Perl srand() is still flawed in default seeding. The changelog for the perl 5.004 release states that the srand() default seed is now based on "a heavy mix of difficult-to-predict system-dependent values." Is that the case, and, if so, what are those values

random numbers and multiple srand calls

偶尔善良 提交于 2019-12-01 17:27:46
I'm writing a program that will generate numerous random numbers in loops. I'm trying to make the numbers somewhat less predictable (not only for security, but to avoid collisions on multiple threads). I've noticed that many documents recommended calling srand only once in the program. For example: Random numbers in C , the selected answer is "As a general rule, only call srand() once in your program". But why? Why would it be so bad to do something like this: int THIS_THREAD_SEED; int randomness() { ++THIS_THREAD_SEED; int i; for(i=0 i<1000; i++) { unsigned n = rand_r(&THIS_THREAD_SEED) /

random numbers and multiple srand calls

允我心安 提交于 2019-12-01 16:32:23
问题 I'm writing a program that will generate numerous random numbers in loops. I'm trying to make the numbers somewhat less predictable (not only for security, but to avoid collisions on multiple threads). I've noticed that many documents recommended calling srand only once in the program. For example: Random numbers in C, the selected answer is "As a general rule, only call srand() once in your program". But why? Why would it be so bad to do something like this: int THIS_THREAD_SEED; int