int main() { int i; sfmt_t sfmt; unsigned long long int tmp; sfmt_init_gen_rand(&sfmt, (unsigned)time(NULL)); // sfmtを用いた乱数の初期化 for (i = 0; i < 100; i++) { printf("%02llx\n", sfmt_genrand_uint64(&sfmt)); } tmp = sfmt_genrand_uint64(&sfmt); printf("%llu\n", tmp); int select[ITEM] = {0}; sfmt_t stmf; for (i = 0; i < 30; i++) { get_rand(select, &sfmt); printf("c : %s\n", int_char(select)); } return 0; }
void SfmtShuffle(LONG *values, LONG size) { uint64_t j; LONG tmp; for (uint64_t i=size-1; i>0; i--) { j = sfmt_genrand_uint64(&GLOBAL_SFMT) % i; tmp = values[j]; values[j] = values[i]; values[i] = tmp; } }
// 64bit + 36bit void get_rand(int select[], sfmt_t *sfmt){ unsigned long int rand_num; int i, s; rand_num = sfmt_genrand_uint64(sfmt); s=0; for (i=0; i<64; i++, s++){ select[s] = rand_num % 2; rand_num /= 2; } rand_num = sfmt_genrand_uint64(sfmt); for (i = 0; i < 36; i++, s++) { select[s] = rand_num % 2; rand_num /= 2; } }
LONG SfmtRand(LONG lo, LONG hi) { return (sfmt_genrand_uint64(&GLOBAL_SFMT) % (hi-lo+1) + lo); }
uint64_t wrap_genrand_uint64(sfmt_t* sfmt) { return sfmt_genrand_uint64(sfmt); }
int main(int argc, char * const argv[]) { char *output = NULL; char buffer[1024]; unsigned long gen = 100; long seed[33]; char c; int mul = 0, seedpos = 0; FILE *out; int verbosity = 1, running = 0; struct timespec tp; char mode[] = "w"; sfmt_t sfmt; int cores = #if defined(WIN32) || defined(__WIN32) 1; #else sysconf (_SC_NPROCESSORS_CONF); #endif unsigned long blocksize; struct process *process; #ifdef _WIN32 seed[0] = time(NULL); #else clock_gettime(CLOCK_REALTIME, &tp); seed[0] = tp.tv_nsec + tp.tv_sec * 1000 * 1000 * 1000; #endif while ((c = getopt(argc, argv, "hc:a:o:s:g:qv")) != -1) { switch (c) { default: usage(argv[0], 1, "ERROR: Unknown parameter %c\n", c); break; case 'h': case '?': usage(argv[0], 0, NULL); break; case 'g': gen = atoi(optarg); if (gen > INT_MAX) usage(argv[0], 2, "ERROR: Generate %lu is over max %d\n", gen, INT_MAX); break; case 'q': verbosity = 0; break; case 'c': cores = atoi(optarg); if (cores <= 0) usage(argv[0], 2, "ERROR: %d cores must be over zero", cores); break; case 'v': verbosity++; break; case 's': { char *startptr = optarg; /* Read seed from /dev/urandom */ if (strcmp("dev", optarg) == 0) { FILE *dev = fopen("/dev/urandom", "r"); int r; if (!dev) { perror("opening /dev/urandom"); return 12; } do { r = fread(seed, sizeof seed[0], sizeof seed/sizeof seed[0], dev); } while (r != sizeof seed/sizeof seed[0] && errno == EINTR); if (r != sizeof seed/sizeof seed[0]) { perror("fread /dev/random"); return 13; } seedpos = sizeof seed/sizeof seed[0]; fclose(dev); break; } for (; seedpos < 33; seedpos++) { char *endptr; unsigned long s; errno = 0; s = strtol(startptr, &endptr, 10); if (endptr == startptr) { break; } if (errno != 0) { perror("sttrol"); return errno; } startptr = endptr + 1; seed[seedpos] = s; if (*endptr == '\0') { seedpos++; break; } } } break; case 'a': mode[0] = 'a'; case 'o': output = strdup(optarg); break; } } freopen("/dev/null", "r", stdin); process = alloca(sizeof(process[0])*cores); memset(process, 0, sizeof(process[0])*cores); /* Calculate reasonable block sizes for the cpu configuration */ blocksize = gen; for (mul = 20; mul > 0; mul--) { long high = 1 << mul; if (gen >= (unsigned)(cores*high)) { blocksize = gen / (cores * mul); break; } } if (blocksize >= (1 << 20)/20) blocksize = (1 << 20)/20; int i, pos = 0; sfmt_init_by_array(&sfmt, (uint32_t*)&seed[0], (seedpos - 1)*(sizeof(seed[0])/sizeof(uint32_t))); pos = sprintf(buffer, "%ld", seed[0]); for (i = 1; i < seedpos; i++) pos += sprintf(&buffer[pos], ",%ld", seed[i]); if (verbosity >= 1) fprintf(stderr, "Generating %ld deals with %s seed. Each subprocess does %ld deals.\n", gen, buffer, blocksize); out = output ? fopen(output, mode) : stdout; if (!out) { perror("Can't open output file"); return 10; } { int fd, maxfd = -1; int c; fd_set fds; fd_set rfds; FD_ZERO(&fds); for (c = 0; c < cores; c++) { unsigned long b = gen - scheduled > blocksize ? blocksize : gen - scheduled; if (b == 0) break; if ((fd = makeprocess(&process[c], b, sfmt_genrand_uint64(&sfmt), verbosity)) < 0) return 20; running++; if (fd > maxfd) maxfd = fd; FD_SET(fd, &fds); } /* Loop as long as we have subprocess active */ while (running > 0) { int active; int p = 0; rfds = fds; /* Wait for any input */ do { active = select(maxfd + 1, &rfds, NULL, NULL, NULL); } while (active == -1 && errno == EINTR); if (active <= 0) { perror("select"); return 60; } /* Check which subprocess provided the input */ for (; p < cores; p++) { if (FD_ISSET(process[p].fd, &rfds)) { errno = 0; while (fgets(buffer, sizeof buffer, process[p].f)) parseLine(buffer, out, &process[p], gen, verbosity); /* Has the process exited? */ if (feof(process[p].f)) { FD_CLR(process[p].fd, &fds); closeprocess(&process[p]); running--; /* Is there more blocks to shedule? */ if (scheduled < gen) { unsigned long b = gen - scheduled > blocksize ? blocksize : gen - scheduled; if ((fd = makeprocess(&process[p], b, sfmt_genrand_uint64(&sfmt), verbosity)) < 0) return 20; running++; if (fd > maxfd) maxfd = fd; FD_SET(fd, &fds); } continue; } /* EWOULDBLOCK and EINTR can happen during normal operation */ if (errno != EWOULDBLOCK && errno != EINTR) { perror("fgets"); return 50; } } } } } fclose(out); return 0; }