I want to create a program that requires only one random number, so I try to use the ARGC address in the main function as a random source, because I think the location of the program in memory is random, and it can also save some include instructions, so I tried :
#include <stdio.h> int main(int argc,const char* argv[]){ printf("%lu\n",(unsigned long int)&argv/sizeof(unsigned long int)); return 0; }
but I found that the output at each point in time is not very "random": they are a multiple of 4:
17591828907268 17591841542404 17591845040388 17591834556676
What is the reason? And does the argc address use a random number?
Then I try to remove some bits of the address:
#include <stdio.h> int main(int argc,const char* argv[]){ printf("%lu\n",(unsigned long int)&argv >> 12); return 0; }
this time it looks pretty random, at least it has both odd and even numbers:
34359070631 34359034616 34359078055 34359080624
Is this the "right" way to turn the argc address into a random number?
source share