| 1 | #include "EXTERN.h" | ||
| 2 | #include "perl.h" | ||
| 3 | #include "XSUB.h" | ||
| 4 | |||
| 5 | #include "ppport.h" | ||
| 6 | |||
| 7 | #include <stdlib.h> | ||
| 8 | #include <sys/time.h> | ||
| 9 | #include <time.h> | ||
| 10 | |||
| 11 | 270 | I32 get_random( pTHX_ IV max_random, SV* s ) { | |
| 12 | struct timeval tv; | ||
| 13 | struct timezone tz; | ||
| 14 | 270 | gettimeofday( &tv, &tz ); | |
| 15 | |||
| 16 | 270 | struct tm *tm = localtime( &tv.tv_sec ); | |
| 17 | 270 | srand( tv.tv_sec + tv.tv_usec ); | |
| 18 | |||
| 19 | 270 | sv_setiv( s, rand() % max_random ); | |
| 20 | 270 | } | |
| 21 | |||
| 22 | MODULE = Scalar::Random PACKAGE = Scalar::Random | ||
| 23 | |||
| 24 | void | ||
| 25 | randomize(SV * target, IV max_random) | ||
| 26 | CODE: | ||
| 27 | struct ufuncs uf; | ||
| 28 | 3 | uf.uf_val = &get_random; | |
| 29 | 3 | uf.uf_set = NULL; | |
| 30 | 3 | uf.uf_index = max_random + 1; | |
| 31 | |||
| 32 | 3 | sv_magic(target, 0, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf)); | |
| 33 | 3 | srand( time(NULL) ); |