dimanche 10 janvier 2016

Can I use the address of argc in main as random source?

I want to create a program that only needs one random number, so I try to use the address of argc in main function as random source because I think the location of the program in memory is random and also it can save some include statements, but 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;
}

the output at each time to print is not very random, but are multiples of 4:

17591828907268
17591841542404
17591845040388
17591834556676

What is the reason? And is using address of argc as random number possible?

Then I try 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;
}

it looks quite random this time, at least it has both odd and even numbers:

34359070631
34359034616
34359078055
34359080624

is that "correct" way to turn the address of argc into random number?




Aucun commentaire:

Enregistrer un commentaire