0
C/C++ Random numbers without time.h header
Hi, I had this idea to use the address of a variable as a seed for the srand() function: https://code.sololearn.com/c4a9A20A10a8 I am wondering if it is viable in the sense that it would produce pseudo-random numbers like the time(0) seed for all compilers, or are there some compilers that would allocate memory for the same variable at the same location every time? Sorry if I sound ignorant, but I was hoping someone with experience could explain what they think.
11 Antworten
+ 2
Martin Taylor, I was hoping you would reply! Thank you for answering, I understand what you are saying. I was just wondering if using this method I proposed for seeding srand would be a better alternative for generating less-predictable random numbers than using the time(0). It seems to me that using the time(0) could yield something predictable, if, in theory, one knew the time the program was run, and the srand formula, they could predict the output of rand(). I am wondering if the address of a variable is inherently harder to predict than the time of day. I imagine it is, but that is why I was hoping you would answer, because, if I remember correctly, you said you dealt heavily with systems implementation. Your thoughts?
+ 2
Martin Taylor, thanks for this response. So it seems that time(0) is in fact a good seed because it is highly variable and wouldn’t be easy to predict.
But I am not sure I agree that the address of a variable results in the same sequence. If you ran the code I posted in this question in the code playground, it produces different numbers every time. I also compiled this on my home computer, and without recompiling, every time I run it I get different numbers as well.
I like this idea of the hardware random number generator because we didn’t create the algorithm for random noise/fluctuations, so to me it seems more plausible. Indeed, they say it is used in cryptography, which makes sense.
I am aware of the C++ random header, which is why I tagged this post as C/C++. I agree it’s by far more comprehensive.
I didn’t know about this high_resolution clock, thanks!
+ 1
Martin Taylor, this is fascinating. If I make foo static or if I put foo outside main, the output doesn't alter. Changing
int foo;
to
int *foo = new int;
doesn't seem to change anything, and putting the heap declaration global is the same as putting the stack declaration global or static. So I guess the point is that the address of a variable is typically fixed for the lifetime of the program. I am now wondering why making a variable static or global will fix the output for multiple runs?
+ 1
Martin Taylor, That makes sense. I guess then my method is flawed in that one can't use it to seed multiple times in the program. I couldn't get the values to change in your for-loop, which is kind of a letdown, but if I understand correctly, you shouldn't be able to based on how memory is allocated on the stack during compilation.
0
Martin Taylor. I believe I found a solution to this apparent problem, instead of using the address of a local variable as a seed, using the address of heap memory makes each line of each run have different output
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
for(int i=0; i<4; i++){
srand((unsigned long long)new int);
//cout << &foo << " ";
for (int i=0; i<10; i++){
cout << rand() % 100 << " ";
}
cout << endl;
}
return 0;
}
0
Martin Taylor. As you had told me, my previous solution had a memory leak, but I think this one does not: https://code.sololearn.com/c8tnS6NEN5x2