Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <stdio.h>
- // Note that this program brute-forces all possible seed values.
- // This may take a fair amount of time - 40 minutes or so in
- // average case, for example, seed 2**32/100 is bruted in 48s, or
- // seed 500000000 in 9:49.
- //
- // If you want to have some progress printing (one character every
- // 1/256 of work done), please compile with DEBUG symbol defined
- // (for example, using -DDEBUG flag during compilation).
- //
- // As for chances of failure: for a seed other than chosen to pass
- // my tests, it would have to produce correct N (1/1000 chance)
- // and have first double within 1e-11 (about 2/1e11 chance). Close
- // to impossible even when multiplied by amount of seeds.
- #define MXN 1000
- #ifdef DEBUG
- #define dprintf(...) printf(__VA_ARGS__)
- #else
- #define dprintf(...)
- #endif
- // This function brute forces the seed. It is able to parallelize well,
- // but doing so messes with main process (dunno why, and don't ask - it's
- // 3AM now :P). So single thread processing it is then.
- void brute(unsigned int id, double x, int N, int all){
- dprintf("Started %d\n", id);
- unsigned int sd=id;
- FILE* f;
- char name[10]="/tmp/0";
- name[5]+=id;
- f=fopen(name, "w");
- do{
- if((sd&0xFFFFFF)==id){
- dprintf("%u",id);
- fflush(stdout);
- }
- srand(sd);
- int myN=(rand()%MXN)+1;
- if(myN!=N){ continue; }
- double rd=(double)rand()/RAND_MAX;
- rd-=x;
- if(rd<0){ rd=-rd; }
- if(rd<1e-11){ // Significantly less than given 15 digits of precision.
- dprintf("\nFound fitting seeed: %u\n", sd);
- fprintf(f, "%u\n", sd);
- fflush(f);
- break;
- }
- }while((sd+=all) != id);
- fclose(f);
- }
- int guess_max(double x, int N, int count) {
- static int done=0;
- static int should;
- if(done){
- dprintf("Called with %d %lf\n", count, x);
- return count==should;
- }
- int nprocesses=1;
- brute(0,x,N,nprocesses);
- unsigned int sd=0;
- for(int i=0;i<nprocesses;i++){
- FILE* f;
- char name[10]="/tmp/0";
- name[5]+=i;
- f=fopen(name, "r");
- while(EOF!=fscanf(f,"%u",&sd));
- fclose(f);
- }
- dprintf("Using seed: %u\n", sd);
- srand(sd);
- int myN=(rand()%MXN)+1;
- double biggest=-1;
- int maxind=-1;
- for(int i=1;i<=myN;i++){
- double rd=(double)rand()/RAND_MAX;
- dprintf("%dth: %lf\n", i, rd);
- if(rd>biggest){
- biggest=rd;
- maxind=i;
- }
- }
- should=maxind;
- done=1;
- dprintf("Should - %d\n", should);
- return maxind==count;
- }
Add Comment
Please, Sign In to add comment