Could you please advise on the g77 fortran equivalent of the routine call gettim(ihr,imin,isec,ihth) particularly to obtain ihth. Or just anything in 9.0 that will generate a random number. Thanks and happy new year
On 29-Dec-03 steve-ss wrote:
Could you please advise on the g77 fortran equivalent of the routine call gettim(ihr,imin,isec,ihth) particularly to obtain ihth. Or just anything in 9.0 that will generate a random number. Thanks and happy new year
It depends on what you want the random numbers for. If you need something "quick and dirty" random for some system purpose, a simple resource is /dev/random which builds up a "noise bank" based on keyboard, mouse etc. activity. Try cat /dev/random > randombytes (you may need to halt it with ^C) and have a look at it with od -x randombytes | less and you will see what I mean. Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding@nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 167 1972 Date: 29-Dec-03 Time: 09:07:07 ------------------------------ XFMail ------------------------------
* steve-ss <mail@steve-ss.com> [Dec 29. 2003 08:10]:
Could you please advise on the g77 fortran equivalent of the routine call gettim(ihr,imin,isec,ihth) particularly to obtain ihth. Or just anything in 9.0 that will generate a random number. Thanks and happy new year
This should do it for you. Compile with gcc -o randomnum randomnum.c. If invoked with an argument that argument is the max value of the randomnum. If no cmdline argument it will default to 2147483647. --> begin randomnum.c <-- #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #include <errno.h> void init_random() { unsigned int seed; FILE *devrandom; devrandom = fopen("/dev/urandom", "r"); if(!devrandom) devrandom = fopen("/dev/random", "r"); if(!devrandom) { perror("Could not open random device"); exit(errno); } seed = (unsigned int)fgetc(devrandom); fclose(devrandom); srand(seed); } int get_random(size_t maxsize) { return (int)(((float)maxsize)*rand()/((float)RAND_MAX));; } int main(int argc, char **argv) { long maxnum = INT32_MAX; if(argc > 1) maxnum = strtol(argv[1], (char **)NULL, 10); init_random(); printf("%d\n", get_random(maxnum)); return 0; } --> end randomnum.c <-- -- Mads Martin Joergensen, http://mmj.dk "Why make things difficult, when it is possible to make them cryptic and totally illogical, with just a little bit more effort?" -- A. P. J.
On Monday 29 December 2003 10:02, Mads Martin Joergensen wrote:
* steve-ss <mail@steve-ss.com> [Dec 29. 2003 08:10]:
Could you please advise on the g77 fortran equivalent of the routine call gettim(ihr,imin,isec,ihth) particularly to obtain ihth. Or just anything in 9.0 that will generate a random number. Thanks and happy new year
This should do it for you. Compile with gcc -o randomnum randomnum.c. If invoked with an argument that argument is the max value of the randomnum. If no cmdline argument it will default to 2147483647.
--> begin randomnum.c <-- #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #include <errno.h>
void init_random() { unsigned int seed; FILE *devrandom;
devrandom = fopen("/dev/urandom", "r"); if(!devrandom) devrandom = fopen("/dev/random", "r");
if(!devrandom) { perror("Could not open random device"); exit(errno); }
seed = (unsigned int)fgetc(devrandom); fclose(devrandom);
srand(seed); }
int get_random(size_t maxsize) { return (int)(((float)maxsize)*rand()/((float)RAND_MAX));; }
int main(int argc, char **argv) { long maxnum = INT32_MAX;
if(argc > 1) maxnum = strtol(argv[1], (char **)NULL, 10);
init_random();
printf("%d\n", get_random(maxnum));
return 0; } --> end randomnum.c <--
Utterly amazing. Thanks to all who replied. Cheers from Steve.
steve-ss <mail@steve-ss.com> writes:
Could you please advise on the g77 fortran equivalent of the routine call gettim(ihr,imin,isec,ihth) particularly to obtain ihth.
IMHO it doesn't exist. You have to call a C function, e.g. clock_gettime(), to get the time with a precision higher than 1 s. -- A.M.
participants (4)
-
Alexandr Malusek
-
Mads Martin Joergensen
-
steve-ss
-
Ted.Harding@nessie.mcc.ac.uk