std::vector<uint64_t> timeGenerator (uint64_t init_time, int num) { std::vector<uint64_t> timeStamp(num); for (int i=0; i<num;++i) { timeStamp[i] = init_time + i * 100 + genRandom(0, 25); } return timeStamp; }
string getString() { string str; for(unsigned int i = 0; i < 4096; ++i) { str += genRandom(); } return str; }
//ffGenerator - frequency-field modelling std::vector<std::pair <uint32_t, uint32_t> > ffGenerator (int num) { std::vector<std::pair <uint32_t, uint32_t> > main (num); for (int x = 0; x < num; ++x) { main[x].first = 280000 + 800 * pow(static_cast<double>(2 + genRandom(1, 10) * 2.0 / 111.0 + sin(x * 3.0 / 2003.0)), 0.9) + 5e-7 * x; //fres, rand model //500*sin(x*2.0/51.0)+x*1.0/20.0;// main[x].second = main[x].first / GYRO; //field (connected by gyromagnetic relation) } return main; }
int main() { big i, n = 100000, A[100000]; srand(time(NULL)); genRandom(A, n); sortBubble(A, n); for (i = 99000; i < 100000; i++) printf("\n%d", A[i]); return 0; }
int run() { while (closeFlag) { sleep(1); MarketUpdate mktUpdt; if (codes.size() == 0) continue; std::string code = codes[genRandom(0, codes.size() - 1)]; mktUpdt.set_code(code); for (int index = 0; index < 10; index++) { mktUpdt.add_bid_price(genRandom(1, 10)); mktUpdt.add_bid_volume(genRandom(1, 10)); mktUpdt.add_ask_price(genRandom(1, 10)); mktUpdt.add_ask_volume(genRandom(1, 10)); } std::string res = ProtoBufHelper::wrapMsg<MarketUpdate>(TYPE_MARKETUPDATE, mktUpdt); msgHub.boardcastMsg(code, res); LOG(INFO) << "boardcast " << res; } }
void logger::CreateLogFile(string const& path) { fstream file(path, ios::in); if (file.is_open()) { cerr << "Journal already exists!" << endl; return; } file.open(path, ios::out | ios::binary);//创建文件 //生成RandomSum并写入 randomSum = genRandom(); file.write((char*)(&randomSum), 4); //写入xChecksum file.close(); }
int main(int argc,char* argv[]){ n = DEFAULT_N; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&size); MPI_Comm_rank(MPI_COMM_WORLD,&rank); if (n%size){ n += size-n%size; } len = n/size; init(); genRandom(); doOddEvenSort(); output(); finz(); MPI_Finalize(); return 0; }
int main() { genRandom(); sa = new(SECURITY_ATTRIBUTES); sa->nLength = sizeof(sa); sa->lpSecurityDescriptor = NULL; sa->bInheritHandle = TRUE; GetSystemInfo(&sysInfo); sysGran = sysInfo.dwAllocationGranularity; HANDLE data; HANDLE fin; fin = CreateEvent(sa, TRUE, FALSE, NULL); data = CreateFile(file, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, sa, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); mapping = CreateFileMapping(data, NULL, PAGE_READWRITE, 0, 0, NULL); pqsarg *arg = new(pqsarg); arg->o = 0; arg->s = sizeof(int) * totalNum; arg->f = fin; b = (int*)MapViewOfFile(mapping, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(int) * totalNum); CreateThread(NULL, 0, pqs, arg, 0, NULL); WaitForSingleObject(fin, INFINITE); UnmapViewOfFile(b); std::ifstream d; d.open(dataFile, std::ifstream::ios_base::binary); int prev, now; d.read((char*)&prev, sizeof(int)); bool isRight = TRUE; for (int i = 1; i < totalNum; i++) { d.read((char*)&now, sizeof(int)); if (now < prev) { isRight = FALSE; break; } prev = now; } if (isRight) std::cout << "The result is right!" << std::endl; else std::cout << "The result is wrong!" << std::endl; return 0; }
void GameScreen::Init(std::string name) { Player *p1 = new Player; p1->Init(name+".png"); p1->SetPosition((float)((Iw2DGetSurfaceWidth()/2)-28),(float)(Iw2DGetSurfaceHeight()/2-28)); p1->SetTarget(p1->GetPosition().x,p1->GetPosition().y); p1->SetVelocity(1,1); Background *bg = new Background; bg->Init("bg.png",(float)Iw2DGetSurfaceWidth(),(float)Iw2DGetSurfaceHeight()); SetBackGround(bg); player = name; warpID = name + genRandom(); AddPlayer(warpID,p1); WarpClientRef->connect(warpID); }
int main(int argc,char* argv[]){ n = DEFAULT_N; mask = DEFAULT_MASK; times = DEFAULT_TIMES; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&size); MPI_Comm_rank(MPI_COMM_WORLD,&rank); if (n%size){ n += size-n%size; } len = n/size; init(); genRandom(); doRadixSort(); output(); finz(); MPI_Finalize(); return 0; }
int main() { //check if top and bottom flipped sf::RenderWindow window = sf::RenderWindow(sf::VideoMode(640, 640, 32), "Perlin Noise", sf::Style::Titlebar | sf::Style::Close); sf::RenderTexture renderTexture; glewExperimental = GL_TRUE; glewInit(); srand(time(NULL)); texture.create(640,640); const float PERSISTENCE = .5; float i = 0; int count = 0; for( i = 2; i < 129; i /= PERSISTENCE){ std::cout << i; sf::Vector2f** gradients; gradients = new sf::Vector2f* [1 + (int)i]; for(int j = 0; j < (int)i + 1; j++){ gradients[j] = new sf::Vector2f[(int)i + 1]; } genRandom(gradients, i, 1 / i); drawNoise(window, gradients, i, 1 / i); for(int j = 0; j < (int)i + 1; j++){ delete[] gradients[j]; } delete[] gradients; //std::cin.get(); } display(window); std::cout << "draw"; window.display(); std::cin.get(); return 0; }
int main(int argc, char *argv[]) { coordinates coord; // Coodinates Structure depth depth; color colorsIn[2048]; // Colors array int numColors = 0; // Number of colors to use in color array float colorPower = .3; // Power exponent to use for color selection int colorStart = 0xFF0000, colorEnd = 0xFFFF00; // Default color gradient settings char *filename = "mandelbrot.png"; // Default PNG output name int i; // Misc variables bool noWindow = false; // Default flag for no-window mode // Set default coordinates before reading in args coord.x = -2; // Default Start Coordinates coord.y = 2; coord.xR = 4; // Default Range Coordinates coord.yR = 4; coord.width = -1; // Invalid pixel size, to be set later coord.height = -1; depth.d = 100; // Default Depth level of Mandelbrot Calculation depth.automatic = true; // Generate default gradient first genGradient(colorsIn, &numColors, colorStart, colorEnd); // Parse Arguments for (i=1; i<argc; i++) { if (strcmp(argv[i], "--width") == 0) { coord.width = arg_check_int(argc, argv, i, 1, 10); i++; } else if (strcmp(argv[i], "--height") == 0) { coord.height = arg_check_int(argc, argv, i, 1, 10); i++; } else if (strcmp(argv[i], "--depth") == 0) { depth.automatic = false; depth.d = arg_check_int(argc, argv, i, 1, 10); i++; } else if (strcmp(argv[i], "--coords") == 0) { coord.x = arg_check_float(argc, argv, i, 1); coord.y = arg_check_float(argc, argv, i, 2); coord.xR = arg_check_float(argc, argv, i, 3); coord.yR = arg_check_float(argc, argv, i, 4); i+=4; } else if (strcmp(argv[i], "--spectrum") == 0) { genSpectrum(colorsIn, &numColors); colorPower = .7; } else if (strcmp(argv[i], "--random") == 0) { genRandom(colorsIn, &numColors); colorPower = 1; } else if (strcmp(argv[i], "--gradient") == 0) { colorStart = arg_check_int(argc, argv, i, 1, 16); colorEnd = arg_check_int(argc, argv, i, 2, 16); genGradient(colorsIn, &numColors, colorStart, colorEnd); i+=2; } else if (strcmp(argv[i], "-o") == 0) { filename = argv[i+1]; i++; } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0 ) { help(); } else if (strcmp(argv[i], "-nw") == 0 ) { noWindow = true; } else { errx(EXIT_FAILURE, "Unknown argument, \"%s\".", argv[i]); } } // Make proportional image if only one dimension is specified // Set to default width and height if not specified if (coord.height < 0 && coord.width > 0) coord.height = coord.width / coord.xR * coord.yR; else if (coord.height > 0 && coord.width < 0) coord.width = coord.height / coord.yR * coord.xR; else if (coord.height < 0 && coord.width < 0) coord.width = coord.height = 1024; coord.xS = coord.xR/coord.width; // X Step Value coord.yS = coord.yR/coord.height; // Y Step Value // Create final array of colors to use that is scaled to the depth that is selected color colors[2048]; scaleColor(colorsIn, colors, numColors, depth.d, colorPower); // If no window mode, just output file and exit if (noWindow) { generate_png(coord, depth.d, filename, colors, numColors); return 0; } // Set up SDL SDL_Init(SDL_INIT_VIDEO); SDL_Window* Main_Window; SDL_Renderer* Main_Renderer; Main_Window = SDL_CreateWindow("Mandelbrot", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, coord.width, coord.height, SDL_WINDOW_RESIZABLE); Main_Renderer = SDL_CreateRenderer(Main_Window, -1, SDL_RENDERER_ACCELERATED); SDL_Texture *texture = SDL_CreateTexture(Main_Renderer, SDL_PIXELFORMAT_RGBX8888, SDL_TEXTUREACCESS_STREAMING, coord.width, coord.height); int pitch; void *pixels; if (SDL_LockTexture(texture, NULL, &pixels, &pitch) != 0) errx(EXIT_FAILURE, "SDL_LockTexture: %s", SDL_GetError()); //uint32_t (*pixel)[coord.height][pitch/sizeof(uint32_t)] = pixels; // Set up struct for tracking mouse movement mouse mouseTracker; mouseTracker.down = false; //Main Loop int maxRender = 2; int needsRender = maxRender; while (1) { // Render Loop if (needsRender > 0) { uint32_t (*pixel)[coord.height][pitch/sizeof(uint32_t)] = pixels; //unsigned long long timer = utime(); //DEBUG - Start Timer float pixelSize = pow(2, --needsRender); //printf("%d %d\n", needsRender, pixelSize); coordinates scaledCoord; scaledCoord.width = coord.width / pixelSize; scaledCoord.height = coord.height / pixelSize; scaledCoord.x = coord.x; scaledCoord.xR = coord.xR; scaledCoord.xS = scaledCoord.xR/scaledCoord.width; scaledCoord.y = coord.y; scaledCoord.yR = coord.yR; scaledCoord.yS = scaledCoord.yR/scaledCoord.height; // y step value SDL_Rect srcrect = {0, 0, scaledCoord.width, scaledCoord.height}; for (int y=0; y<scaledCoord.height; y++) for (int x=0; x<scaledCoord.width; x++) { double xValue = scaledCoord.x + (x * scaledCoord.xS); double yValue = scaledCoord.y - (y * scaledCoord.yS); int result = mandel(xValue, yValue, depth.d); int finalColor = 0; if (result != -1) finalColor = colors[result].hex << 8; (*pixel)[y][x] = finalColor; } SDL_UnlockTexture(texture); SDL_RenderCopy(Main_Renderer, texture, &srcrect, NULL); //printf("%llu - Finish Render\n", utime()-timer); //DEBUG - End Timer SDL_RenderPresent(Main_Renderer); } SDL_Event e; //SDL_WaitEvent(&e); if (SDL_WaitEventTimeout(&e, 10) == 0) continue; if (e.type == SDL_MOUSEWHEEL) { if (e.wheel.y > 0) coord_zoom(&coord, 1); else coord_zoom(&coord, -1); if (depth.automatic == true) { adjust_depth(&coord, &depth); scaleColor(colorsIn, colors, numColors, depth.d, colorPower); } needsRender = maxRender; } if (e.type == SDL_WINDOWEVENT) if (e.window.event == SDL_WINDOWEVENT_RESIZED) { // Adjust view to new size coord.width = e.window.data1; coord.height = e.window.data2; coord.xR = coord.width * coord.xS; coord.yR = coord.height * coord.yS; // Destroy old texture SDL_DestroyTexture(texture); // Make New Texture texture = SDL_CreateTexture(Main_Renderer, SDL_PIXELFORMAT_RGBX8888, SDL_TEXTUREACCESS_STREAMING, coord.width, coord.height); // Lock with new texture if (SDL_LockTexture(texture, NULL, &pixels, &pitch) != 0) errx(EXIT_FAILURE, "SDL_LockTexture: %s", SDL_GetError()); // Rerender needsRender = maxRender; } if (e.type == SDL_MOUSEBUTTONDOWN) if (e.button.state == SDL_PRESSED) { mouseTracker.mouseX = e.motion.x; mouseTracker.mouseY = e.motion.y; mouseTracker.coordX = coord.x; mouseTracker.coordY = coord.y; mouseTracker.down = true; } if (e.type == SDL_MOUSEBUTTONUP) if (e.button.state == SDL_RELEASED) mouseTracker.down = false; if (e.type == SDL_MOUSEMOTION && mouseTracker.down == true) { coord.x = mouseTracker.coordX + ((mouseTracker.mouseX - e.motion.x) * coord.xS); coord.y = mouseTracker.coordY - ((mouseTracker.mouseY - e.motion.y) * coord.yS); needsRender = maxRender; } if (e.type == SDL_KEYUP) { if (e.key.keysym.sym == SDLK_p) print_coords(&coord, &depth); if (e.key.keysym.sym == SDLK_s) generate_png(coord, depth.d, filename, colors, numColors); if (e.key.keysym.sym == SDLK_EQUALS || e.key.keysym.sym == SDLK_MINUS) { depth.automatic = false; if (e.key.keysym.sym == SDLK_EQUALS) depth.d += 5; if (e.key.keysym.sym == SDLK_MINUS) if (depth.d > 5) depth.d -= 5; scaleColor(colorsIn, colors, numColors, depth.d, colorPower); needsRender = maxRender; } } if (e.type == SDL_QUIT) { SDL_Log("Program quit after %i ticks", e.quit.timestamp); break; } } return 0; }
void genTestCase(){ genRandom("rnd.dat"); genSequence("seq.dat"); }
void run() { unsigned cores = std::thread::hardware_concurrency() - 1; std::vector<std::future<int> > futures; std::stack<int> data; for(int i = 0; i <=100; i++) data.push(i); int res = 0; auto worker = [](int from, int to)-> int { // for (int i = 0; i < 50000; i++) { // for (int j = 0; j < 50000; j++) { // int x = i - j; // } // } int n = genRandom(from, to); fib(n); std::cout << to << std::endl; return to; }; while (data.size() > 0) { // pull new data int v = data.top(); data.pop(); // fork a thread if there are available cores. if (futures.size() < cores) { auto f = std::async(std::launch::async, worker, 0, v); futures.push_back(std::move(f)); } // loop to check if any task is done. // if any core is available, fork a new task. int index = 0; while (futures.size() > 0) { if (futures.size() < cores) { break; } std::future<int>& f = futures[index]; std::future_status st = f.wait_for(std::chrono::milliseconds(10)); if (st == std::future_status::ready) { res += f.get(); futures.erase(futures.begin() + index); break; // break out to push another task. } index++; if (index >= futures.size()) { index = 0; } } } // loop to wait for the last tasks to finish. int index = 0; while (futures.size() > 0) { std::future<int>& f = futures[index]; std::future_status st = f.wait_for(std::chrono::milliseconds(10)); if (st == std::future_status::ready) { res += f.get(); futures.erase(futures.begin() + index); } index++; if (index >= futures.size()) { index = 0; } } std::cout << "total: " << res << std::endl; }