static void DoTurn(const Game& pw) { typedef std::vector<Planet> Planets; // (1) If we current have a fleet in flight, then do nothing until it // arrives. if (pw.MyFleets().size() >= 1) { return; } // (2) Pick one of my planets at random. int source = -1; Planets p = pw.MyPlanets(); if (p.size() > 0) { source = NextRand(p.size()); } // (3) Pick a target planet at random. int dest = -1; p = pw.Planets(); if (p.size() > 0) { dest = NextRand(p.size()); } // (4) Send half the ships from source to dest. if (source >= 0 && dest >= 0 && source != dest) { int numShips = pw.state.planets[source].numShips / 2; pw.IssueOrder(source, dest, numShips); } }
/* * double UnifReal( double dLow, double dHigh, long nStream ) */ double UnifReal(double dLow, double dHigh, long nStream) /* * Returns a double uniformly distributed between dLow and dHigh, * excluding the endpoints. nStream is the random number stream. * Stream 0 is used if nStream is not in the range 0..MAX_STREAM. */ { double dTemp; if (nStream < 0 || nStream > MAX_STREAM) nStream = 0; if (dLow == dHigh) return (dLow); if (dLow > dHigh) { dTemp = dLow; dLow = dHigh; dHigh = dTemp; } Seed[nStream] = NextRand(Seed[nStream]); dTemp = ((double) Seed[nStream] / dM) * (dHigh - dLow); return (dLow + dTemp); }
/* * long UnifInt( long nLow, long nHigh, long nStream ) */ long UnifInt(long nLow, long nHigh, long nStream) /* * Returns an integer uniformly distributed between nLow and nHigh, * including * the endpoints. nStream is the random number stream. * Stream 0 is used if nStream is not in the range 0..MAX_STREAM. */ { double dRange; long nTemp; if (nStream < 0 || nStream > MAX_STREAM) nStream = 0; if (nLow == nHigh) return (nLow); if (nLow > nHigh) { nTemp = nLow; nLow = nHigh; nHigh = nTemp; } dRange = (double) (nHigh - nLow + 1); Seed[nStream] = NextRand(Seed[nStream]); nTemp = (long) (((double) Seed[nStream] / dM) * (dRange)); return (nLow + nTemp); }
/* * double Exponential( double dMean, long nStream ) */ double Exponential(double dMean, long nStream) /* * Returns a double uniformly distributed with mean dMean. * 0.0 is returned iff dMean <= 0.0. nStream is the random number * stream. Stream 0 is used if nStream is not in the range * 0..MAX_STREAM. */ { double dTemp; if (nStream < 0 || nStream > MAX_STREAM) nStream = 0; if (dMean <= 0.0) return (0.0); Seed[nStream] = NextRand(Seed[nStream]); dTemp = (double) Seed[nStream] / dM; /* unif between 0..1 */ return (-dMean * log(1.0 - dTemp)); }
/* * long UnifInt( long nLow, long nHigh, long nStream ) */ DSS_HUGE UnifInt(DSS_HUGE nLow, DSS_HUGE nHigh, long nStream) /* * Returns an integer uniformly distributed between nLow and nHigh, * including * the endpoints. nStream is the random number stream. * Stream 0 is used if nStream is not in the range 0..MAX_STREAM. */ { double dRange; DSS_HUGE nTemp, nRange; int32_t nLow32 = (int32_t)nLow, nHigh32 = (int32_t)nHigh; if (nStream < 0 || nStream > MAX_STREAM) nStream = 0; if ((nHigh == MAX_LONG) && (nLow == 0)) { dRange = DOUBLE_CAST (nHigh32 - nLow32 + 1); nRange = nHigh32 - nLow32 + 1; } else { dRange = DOUBLE_CAST (nHigh - nLow + 1); nRange = nHigh - nLow + 1; } Seed[nStream].value = NextRand(Seed[nStream].value); #ifdef RNG_TEST Seed[nStream].nCalls += 1; #endif nTemp = (DSS_HUGE) (((double) Seed[nStream].value / dM) * (dRange)); return (nLow + nTemp); }
long SkewInt(long nLow, long nHigh, long nStream, double skewVal, long n) /* * */ { double zipf; double dRange; long nTemp; double multiplier; double Czn; long numDistinctValuesGenerated; /* check for validity of skewVal */ if(skewVal < 0 || skewVal > 5) zipf = 0; /* assume uniform */ else if(skewVal==5.0) { /* special case */ /* check if any values have been generated for this column */ if(NumDistinctValuesGenerated[nStream]==0) { /* pick a skew value to be used for this column*/ zipf = (int) UnifInt(0, 4, 0); ColumnSkewValue[nStream] = zipf; } else { /* column skew value has been initialized before */ zipf = ColumnSkewValue[nStream]; } } else { /* skewVal is between 0 and 4 */ zipf = skewVal; } /* If no values have been generated for this stream as yet, get multiplier */ if(NumDistinctValuesGenerated[nStream]==0) { Multiplier[nStream] = GetMultiplier(n, zipf); } multiplier = Multiplier[nStream]; /* * Check how many copies of the current value * have already been generated for this stream. * If we have generated enough, proceed to * next value, and decide how many copies of * this value should be generated. */ if(CurrentValueCounter[nStream] == CurrentValueTarget[nStream]) { /* proceed to next value */ if (nStream < 0 || nStream > MAX_STREAM) nStream = 0; if (nLow == nHigh) nTemp = nLow; else { if (nLow > nHigh) { nTemp = nLow; nLow = nHigh; nHigh = nTemp; } dRange = (double) (nHigh - nLow + 1); Seed[nStream] = NextRand(Seed[nStream]); nTemp = (long) (((double) Seed[nStream] / dM) * (dRange)); nTemp += nLow; CurrentValue[nStream] = nTemp; } } else { /* return another copy of current value */ nTemp = CurrentValue[nStream]; CurrentValueCounter[nStream]++; return nTemp; } /* * check how many distinct values for this column * have already been generated. */ numDistinctValuesGenerated = NumDistinctValuesGenerated[nStream] + 1; if(n<1) n = (nHigh - nLow + 1); /* * zipf is guaranteed to be in the range 0..4 * pick the multiplier if(zipf == 0) multiplier = n; else if(zipf==1) multiplier = log(n) + 0.577 ; else if(zipf==2) multiplier = (PI*PI)/6.0; else if(zipf==3) multiplier = 1.202; else if(zipf==4) multiplier = (PI*PI*PI*PI)/90.0; */ Czn = n/multiplier; CurrentValueTarget[nStream]= (long) (Czn/pow((double)numDistinctValuesGenerated, zipf)); /* ensure that there is at least one value*/ CurrentValueTarget[nStream] = (CurrentValueTarget[nStream] < 1) ? 1: CurrentValueTarget[nStream]; CurrentValueCounter[nStream] = 1; NumDistinctValuesGenerated[nStream]++; return nTemp; }
main(int ac, char **av) { int i; FILE *ifp; char line[LINE_SIZE]; prog = av[0]; flt_scale = (double)1.0; flags = 0; d_path = NULL; process_options(ac, av); if (flags & VERBOSE) fprintf(ofp, "-- TPC %s Parameter Substitution (Version %d.%d.%d%s)\n", NAME, VERSION, RELEASE, MODIFICATION, PATCH); setup(); if (!(flags & DFLT)) /* perturb the RNG */ { if (!(flags & SEED)) rndm = (long)((unsigned)time(NULL) * DSS_PROC); if (rndm < 0) rndm += 2147483647; Seed[0].value = rndm; for (i=1; i <= QUERIES_PER_SET; i++) { Seed[0].value = NextRand(Seed[0].value); Seed[i].value = Seed[0].value; } printf("-- using %ld as a seed to the RNG\n", rndm); } else printf("-- using default substitutions\n"); if (flags & INIT) /* init stream with ifile */ { ifp = fopen(ifile, "r"); OPEN_CHECK(ifp, ifile); while (fgets(line, LINE_SIZE, ifp) != NULL) fprintf(stdout, "%s", line); } if (snum >= 0) if (optind < ac) for (i=optind; i < ac; i++) { char qname[10]; sprintf(qname, "%d", SEQUENCE(snum, atoi(av[i]))); qsub(qname, flags); } else for (i=1; i <= QUERIES_PER_SET; i++) { char qname[10]; sprintf(qname, "%d", SEQUENCE(snum, i)); qsub(qname, flags); } else if (optind < ac) for (i=optind; i < ac; i++) qsub(av[i], flags); else for (i=1; i <= QUERIES_PER_SET; i++) { char qname[10]; sprintf(qname, "%d", i); qsub(qname, flags); } if (flags & TERMINATE) /* terminate stream with tfile */ { ifp = fopen(tfile, "r"); if (ifp == NULL) OPEN_CHECK(ifp, tfile); while (fgets(line, LINE_SIZE, ifp) != NULL) fprintf(stdout, "%s", line); } return(0); }