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.