📄 randtest.c
字号:
counter = 0; cout << NFLIPS << " calls to GARandomDouble(0,5)\t"; for(i=0; i<NFLIPS; i++) counter += GARandomDouble(0,5); mean = counter/(double)NFLIPS; cout << mean << " (should be about 2.5)\n"; cout.flush(); counter = 0; cout << NFLIPS << " calls to GARandomDouble(-10,-5)\t"; for(i=0; i<NFLIPS; i++) counter += GARandomDouble(-10,-5); mean = counter/(double)NFLIPS; cout << mean << " (should be about -7.5)\n"; cout.flush(); counter = 0; cout << NFLIPS << " calls to GARandomDouble(-10,10)\t"; for(i=0; i<NFLIPS; i++) counter += GARandomDouble(-10,10); mean = counter/(double)NFLIPS; cout << mean << " (should be about 0.0)\n"; cout.flush(); // check means of repeated coin tosses cout << "\n"; float ave; cout << "[0, 1] with GARandomFloat()...\t\t"; ave = 0; for(i=0; i<50; i++) ave += GARandomFloat(); cout << ave/50 << " "; ave = 0; for(i=0; i<50; i++) ave += GARandomFloat(); cout << ave/50 << " "; ave = 0; for(i=0; i<50; i++) ave += GARandomFloat(); cout << ave/50 << "\n"; cout.flush(); cout << "[0, 1] with GARandomFloat(0,1)...\t"; ave = 0; for(i=0; i<50; i++) ave += GARandomFloat(0,1); cout << ave/50 << " "; ave = 0; for(i=0; i<50; i++) ave += GARandomFloat(0,1); cout << ave/50 << " "; ave = 0; for(i=0; i<50; i++) ave += GARandomFloat(0,1); cout << ave/50 << "\n"; cout.flush(); cout << "[0, 100] with GARandomFloat(0,100)...\t"; ave = 0; for(i=0; i<50; i++) ave += GARandomFloat(0,100); cout << ave/50 << " "; ave = 0; for(i=0; i<50; i++) ave += GARandomFloat(0,100); cout << ave/50 << " "; ave = 0; for(i=0; i<50; i++) ave += GARandomFloat(0,100); cout << ave/50 << "\n"; cout.flush(); double aved; cout << "[0, 1] with GARandomDouble()...\t\t"; aved = 0; for(i=0; i<50; i++) aved += GARandomDouble(); cout << aved/50 << " "; aved = 0; for(i=0; i<50; i++) aved += GARandomDouble(); cout << aved/50 << " "; aved = 0; for(i=0; i<50; i++) aved += GARandomDouble(); cout << aved/50 << "\n"; cout.flush(); cout << "[0, 1] with GARandomDouble(0,1)...\t"; aved = 0; for(i=0; i<50; i++) aved += GARandomDouble(0,1); cout << aved/50 << " "; aved = 0; for(i=0; i<50; i++) aved += GARandomDouble(0,1); cout << aved/50 << " "; aved = 0; for(i=0; i<50; i++) aved += GARandomDouble(0,1); cout << aved/50 << "\n"; cout.flush(); cout << "[0, 100] with GARandomDouble(0,100)...\t"; aved = 0; for(i=0; i<50; i++) aved += GARandomDouble(0,100); cout << aved/50 << " "; aved = 0; for(i=0; i<50; i++) aved += GARandomDouble(0,100); cout << aved/50 << " "; aved = 0; for(i=0; i<50; i++) aved += GARandomDouble(0,100); cout << aved/50 << "\n"; cout.flush(); cout << "\n"; int count; cout << "1% coin flip...\t\t"; count = 0; for(i=0; i<NFLIPS; i++) count += GAFlipCoin(0.01); counter = (double)count / (double)i; cout << counter << " "; count = 0; for(i=0; i<NFLIPS; i++) count += GAFlipCoin(0.01); counter = (double)count / (double)i; cout << counter << " "; count = 0; for(i=0; i<NFLIPS; i++) count += GAFlipCoin(0.01); counter = (double)count / (double)i; cout << counter << "\n"; cout.flush(); cout << "25% coin flip...\t"; count = 0; for(i=0; i<NFLIPS; i++) count += GAFlipCoin(0.25); counter = (double)count / (double)i; cout << counter << " "; count = 0; for(i=0; i<NFLIPS; i++) count += GAFlipCoin(0.25); counter = (double)count / (double)i; cout << counter << " "; count = 0; for(i=0; i<NFLIPS; i++) count += GAFlipCoin(0.25); counter = (double)count / (double)i; cout << counter << "\n"; cout.flush(); cout << "50% coin flip...\t"; count = 0; for(i=0; i<NFLIPS; i++) count += GAFlipCoin(0.5); counter = (double)count / (double)i; cout << counter << " "; count = 0; for(i=0; i<NFLIPS; i++) count += GAFlipCoin(0.5); counter = (double)count / (double)i; cout << counter << " "; count = 0; for(i=0; i<NFLIPS; i++) count += GAFlipCoin(0.5); counter = (double)count / (double)i; cout << counter << "\n"; cout.flush(); cout << "75% coin flip...\t"; count = 0; for(i=0; i<NFLIPS; i++) count += GAFlipCoin(0.75); counter = (double)count / (double)i; cout << counter << " "; count = 0; for(i=0; i<NFLIPS; i++) count += GAFlipCoin(0.75); counter = (double)count / (double)i; cout << counter << " "; count = 0; for(i=0; i<NFLIPS; i++) count += GAFlipCoin(0.75); counter = (double)count / (double)i; cout << counter << "\n"; cout.flush(); cout << "99% coin flip...\t"; count = 0; for(i=0; i<NFLIPS; i++) count += GAFlipCoin(0.99); counter = (double)count / (double)i; cout << counter << " "; count = 0; for(i=0; i<NFLIPS; i++) count += GAFlipCoin(0.99); counter = (double)count / (double)i; cout << counter << " "; count = 0; for(i=0; i<NFLIPS; i++) count += GAFlipCoin(0.99); counter = (double)count / (double)i; cout << counter << "\n"; cout.flush(); long lcount; cout << "\ncalls to GARandomBit (these should yield 50%)...\n"; lcount = 0; for(i=0; i<NFLIPS; i++) lcount += GARandomBit(); counter = (double)lcount / (double)i; cout << counter << " "; lcount = 0; for(i=0; i<NFLIPS; i++) lcount += GARandomBit(); counter = (double)lcount / (double)i; cout << counter << " "; lcount = 0; for(i=0; i<NFLIPS; i++) lcount += GARandomBit(); counter = (double)lcount / (double)i; cout << counter << "\n"; cout.flush(); }// check for values outside of bounds if(dobnds) { cout << "\n"; cerr << "bounds test...\n"; cout << "check for out of bounds...\n"; long int ii, err; int vali; cerr << " " << NUM_CHECKS << " GARandomInt(-10,5)...\n"; cout << "GARandomInt(-10,5) ..."; cout.flush(); err = 0; for(ii=0; ii<NUM_CHECKS; ii++){ vali = GARandomInt(-10,5); if(vali < -10 || vali > 5){ err += 1; } } if(err) cout << " " << err << " values out of bounds.\n"; else cout << "ok\n"; cout.flush(); cerr << " " << NUM_CHECKS << " GARandomInt(0,3)...\n"; cout << "GARandomInt(0,3) ..."; cout.flush(); err = 0; for(ii=0; ii<NUM_CHECKS; ii++){ vali = GARandomInt(0,3); if(vali < 0 || vali > 3){ err += 1; } } if(err) cout << " " << err << " values out of bounds.\n"; else cout << "ok\n"; cout.flush(); cerr << " " << NUM_CHECKS << " GARandomInt(200,255)...\n"; cout << "GARandomInt(200,255) ..."; cout.flush(); err = 0; for(ii=0; ii<NUM_CHECKS; ii++){ vali = GARandomInt(200,255); if(vali < 200 || vali > 255){ err += 1; } } if(err) cout << " " << err << " values out of bounds.\n"; else cout << "ok\n"; cout.flush(); float valf, lastf=0.0; cerr << " " << NUM_CHECKS << " GARandomFloat(-10,5)...\n"; cout << "GARandomFloat(-10,5) ..."; cout.flush(); for(ii=0; ii<NUM_CHECKS; ii++){ valf = GARandomFloat(-10,5); if(valf < -10 || valf > 5){ err += 1; lastf = valf; } } if(err) cout << " " << err << " values out of bounds (" << lastf << ").\n"; else cout << "ok\n"; cout.flush(); cerr << " " << NUM_CHECKS << " GARandomFloat(0,3)...\n"; cout << "GARandomFloat(0,3) ..."; cout.flush(); err = 0; for(ii=0; ii<NUM_CHECKS; ii++){ valf = GARandomFloat(0,3); if(valf < 0 || valf > 3){ err += 1; lastf = valf; } } if(err) cout << " " << err << " values out of bounds (" << lastf << ").\n"; else cout << "ok\n"; cout.flush(); cerr << " " << NUM_CHECKS << " GARandomFloat(200,255)...\n"; cout << "GARandomFloat(200,255) ..."; cout.flush(); err = 0; for(ii=0; ii<NUM_CHECKS; ii++){ valf = GARandomFloat(200,255); if(valf < 200 || valf > 255){ err += 1; lastf = valf; } } if(err) cout << " " << err << " values out of bounds (" << lastf << ").\n"; else cout << "ok\n"; cout.flush(); double vald, lastd=0.0; cerr << " " << NUM_CHECKS << " GARandomDouble(-10,5)...\n"; cout << "GARandomDouble(-10,5) ..."; cout.flush(); for(ii=0; ii<NUM_CHECKS; ii++){ vald = GARandomDouble(-10,5); if(vald < -10 || vald > 5){ err += 1; lastd = vald; } } if(err) cout << " " << err << " values out of bounds (" << lastd << ").\n"; else cout << "ok\n"; cout.flush(); cerr << " " << NUM_CHECKS << " GARandomDouble(0,3)...\n"; cout << "GARandomDouble(0,3) ..."; cout.flush(); err = 0; for(ii=0; ii<NUM_CHECKS; ii++){ vald = GARandomDouble(0,3); if(vald < 0 || vald > 3){ err += 1; lastd = vald; } } if(err) cout << " " << err << " values out of bounds (" << lastd << ").\n"; else cout << "ok\n"; cout.flush(); cerr << " " << NUM_CHECKS << " GARandomDouble(200,255)...\n"; cout << "GARandomDouble(200,255) ..."; cout.flush(); err = 0; for(ii=0; ii<NUM_CHECKS; ii++){ vald = GARandomDouble(200,255); if(vald < 200 || vald > 255){ err += 1; lastd = vald; } } if(err) cout << " " << err << " values out of bounds (" << lastd << ").\n"; else cout << "ok\n"; cout.flush(); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -