This page shows how to get an argument and pass it to
function srand as its seed.
It is implemented as part of a c++
series of exercises
, that I did.
-
In
ECC
project I need to generate random numbers. I first used
time for the job. Since the program ends quickly, in
regression from script, this was not too good. So I have
moved the seed generation to the script and used arg[cv].
- #include <time.h>
- ...
- int main(int argc, char *argv[]) {
- ...
- if(argc == 1)
- srand ( time(NULL) );
- else {
- seed=atoi(argv[1]);
- srand (seed);
- #ifdef DBG_3 //{
- cout << "argv " << argv[1] << " seed " << seed << endl;
- #endif //}
- }
The bash script uses the variable RANDOM to generate random
seed.
for i in {1..500} ; do
./a.out $RANDOM >> ecc_main_1.txt
|