本文整理汇总了C++中Random函数的典型用法代码示例。如果您正苦于以下问题:C++ Random函数的具体用法?C++ Random怎么用?C++ Random使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Random函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: FlyProcess
protected func FlyProcess() {
Smoke(0, 0, 7 + Random(5));
if (Stuck() || (GetActTime() > 150))
return(Hit());
}
示例2: Random
void DistributionBranch::MutateAttributes()
{
Random(1);
return;
}
示例3: main
//.........这里部分代码省略.........
epsilon[i][j] = (sqrt(epsilontemp[i]*epsilontemp[j]));
}
}
free(sigmatemp);
free(epsilontemp);
mave = mave/N;
printf("Initializing positions...");
if (init_type == 0 ) { //Test on one particle - put it in the middle of the box.
for(i=0; i<N; i++) {
p[i].x=0.0;
p[i].y=0.0;
p[i].z=0.0;
}
}
if(init_type == 1) { //Complete this for LAMMPS - style frame
printf("Initialization type not yet implemented");
return 1;
FILE* input_coords_file;
if ((input_coords_file = fopen(argv[1],"r"))==NULL) {
printf("Error opening %s\n",argv[1]);
return 1;
}
fclose(input_coords_file);
}
if(init_type == 2) { //Random
initializeRand(N,L);
}
if(init_type == 3) { //Lattice
initializeLattice(N,L);
}
if(init_type == 4) { //LAttice plus perturb.
printf("Initialization type not yet implemented");
return 1;
}
//Initialize forces
for (i=0; i<N; i++) {
p[i].fx = 0.0;
p[i].fy = 0.0;
p[i].fz = 0.0;
}
printf("Initializing velocities...");
//Randomly assign velocities from continous distribution [-vrange,+vrange], compute total velocity and correct momentum.
//Calculate vrange from input T assuming uniform distribution
T = T_in/epsilon_scale; //some base epsilon, all epsilons in epsilon array are multiples of this epsilon_scale.
double vrange = sqrt(T*2.0/mave); //Vrange is range of the uniform distribution with rms v^2, solve using KE = 3NT/2 = 3Nmvrange^2/4 from v_ave = (2vrange)^2/12. Using averge mass: is this correct?
示例4: Shiver
protected func Shiver()
{
// Bewegung
SetXDir(Random(21) - 10);
return(1);
}
示例5: main
int main () {
int i, j, n, r, p, deckCount, discardCount, handCount, handPos, randomCard, randomHandCount;
int k[10] = {adventurer, council_room, feast, gardens, mine,
remodel, smithy, village, baron, great_hall};
struct gameState G;
printf ("Testing CARD SMITHY.\n");
SelectStream(2);
PutSeed(3);
for (n = 0; n < 1; n++) {
for (i = 0; i < sizeof(struct gameState); i++) {
((char*)&G)[i] = floor(Random() * 256);
}
p = floor(Random() * 2);
randomHandCount = floor(Random() * MAX_HAND);
G.deckCount[p] = floor(Random() * MAX_DECK);
G.discardCount[p] = floor(Random() * MAX_DECK);
G.handCount[p] = randomHandCount;
/* initialize hand */
// give the player real cards
for ( j=0; j< randomHandCount; j++)
{
G.hand[p][j] = 0;
randomCard = floor(Random() * 3);
switch (randomCard) {
case 0:
G.hand[p][j] = copper;
break;
case 1:
G.hand[p][j] = silver;
break;
case 2:
G.hand[p][j] = gold;
break;
}
}
/* player two now has real cards */
for ( j=0; j< randomHandCount; j++)
{
G.hand[1-p][j] = 0;
randomCard = floor(Random() * 3);
switch (randomCard) {
case 0:
G.hand[1-p][j] = copper;
break;
case 1:
G.hand[1-p][j] = silver;
break;
case 2:
G.hand[1-p][j] = gold;
break;
}
}
G.whoseTurn = 0;
handPos = 0;
handPos = 5;
G.deckCount[p] = 40;
G.discardCount[p] = 10;
G.handCount[p] = 8;
G.discard[p][G.discardCount[p]] = 100;
G.playedCardCount = 3;
G.playedCards[ G.playedCardCount ] = G.hand[p][handPos-1];
r = checkSmithy(&G, handPos, p);
}
printf ("ALL TESTS OK FOR SMITHY \n\n");
exit(0);
return 0;
}
示例6: sqrt
void
ReptationMC::moveReptile(){
//RealType oneovertau = 1.0/Tau;
//RealType oneover2tau = 0.5*oneovertau;
RealType tauover2 = 0.5*Tau;
RealType g = sqrt(Tau);
typedef MCWalkerConfiguration::PropertyContainer_t PropertyContainer_t;
if(!UseBounce && Random()<0.5) {
Reptile->flip();
NumTurns++;
}
Walker_t* anchor = Reptile->makeEnds();
//save the local energies of the anchor and tails
//eloc_xp = the energy of the front
//eloc_yp = the energy of the proposed move
//eloc_x = the energy of the tail
//eloc_y = the energy of the tail-1
RealType eloc_xp = anchor->Properties(LOCALENERGY);
RealType eloc_x = Reptile->tails[0]->Properties(LOCALENERGY);
RealType eloc_y = Reptile->tails[1]->Properties(LOCALENERGY);
NumCuts = Reptile->NumCuts;
RealType Wpolymer=0.0;
for(int i=0; i<NumCuts; ) {
Walker_t* head=Reptile->heads[i];
//create a 3N-Dimensional Gaussian with variance=1
makeGaussRandom(deltaR);
W.R = anchor->R + g*deltaR + Tau* anchor->Drift;
//update the distance table associated with W
//DistanceTable::update(W);
W.update();
//evaluate wave function
ValueType logpsi(Psi.evaluateLog(W));
//update the properties of the front chain
//RealType eloc_yp = head->Properties(LOCALENERGY) = H.evaluate(W);
//H.copy(head->getEnergyBase());
//head->Properties(LOCALPOTENTIAL) = H.getLocalPotential();
RealType eloc_yp = H.evaluate(W);
head->resetProperty(logpsi,Psi.getSign(),eloc_yp);
H.saveProperty(head->getPropertyBase());
head->R = W.R;
//ValueType vsq = Dot(W.G,W.G);
//ValueType scale = ((-1.0+sqrt(1.0+2.0*Tau*vsq))/vsq);
//head->Drift = scale*W.G;
head->Drift = W.G;
//\f${x-y-\tau\nabla \ln \Psi_{T}(y))\f$
//deltaR = anchor->R - W.R - heads[i]->Drift;
//Gdrift *= exp(-oneover2tau*Dot(deltaR,deltaR));
/*
\f$ X= \{R_0, R_1, ... , R_M\}\f$
\f$ X' = \{R_1, .., R_M, R_{M+1}\}\f$
\f[ G_B(R_{M+1}\leftarrow R_{M}, \tau)/G_B(R_{0}\leftarrow R_{1}, \tau)
= exp\(-\tau/2[E_L(R_{M+1})+E_L(R_M)-E_L(R_1)-E_L(R_0)]\)\f]
*
- eloc_yp = \f$E_L(R_{M+1})\f$
- eloc_xp = \f$E_L(R_{M})\f$
- eloc_y = \f$E_L(R_{1})\f$
- eloc_x = \f$E_L(R_{0})\f$
*/
//Wpolymer *= exp(-oneover2tau*(eloc_yp+eloc_xp-eloc_x-eloc_y));
Wpolymer +=(eloc_yp+eloc_xp-eloc_x-eloc_y);
//move the anchor and swap the local energies for Wpolymer
anchor=head;
//increment the index
i++;
if(i<NumCuts) {
eloc_xp = eloc_yp;
eloc_x = eloc_y;
eloc_y = Reptile->tails[i+1]->Properties(LOCALENERGY);
}
}
Wpolymer = exp(-tauover2*Wpolymer);
double accept = std::min(1.0,Wpolymer);
if(Random() < accept){//move accepted
Reptile->updateEnds();
++nAccept;
} else {
++nReject;
if(UseBounce) {
NumTurns++;
Reptile->flip();
}
}
//RealType Bounce = UseBounce ? 1.0-accept: 0.5;
//.........这里部分代码省略.........
示例7: Initialize
public func Initialize() {
SetGamma(RGB(0,0,0),RGB(68,68,83),RGB(147,147,173));
for(var y=0; y<LandscapeHeight(); (y+=510) && (x=0))
for(var x=0; x<LandscapeWidth(); x+=530)
ObjectSetAction(CreateObject(_CSN,x,y,-1),Format("Wait%d",Random(4)));
}
示例8: sin
void cSSAOWorker::doWork()
{
int quality = threadData->quality;
int startLine = threadData->startLine;
int width = image->GetWidth();
int height = image->GetHeight();
double *cosine = new double[quality];
double *sine = new double[quality];
for (int i = 0; i < quality; i++)
{
sine[i] = sin((double)i / quality * 2.0 * M_PI);
cosine[i] = cos((double)i / quality * 2.0 * M_PI);
}
double scale_factor = (double)width / (quality * quality) / 2.0;
double aspectRatio = (double)width / height;
params::enumPerspectiveType perspectiveType = params->perspectiveType;
double fov = params->fov;
double intensity = params->ambientOcclusion;
int listIndex = 0;
int step = threadData->progressive;
if (step == 0) step = 1;
for (int y = startLine; y < height; y += threadData->noOfThreads)
{
if (threadData->list)
{
if (listIndex >= threadData->list->size())
{
continue;
}
if (y < threadData->list->at(listIndex))
{
continue;
}
else
{
listIndex++;
}
}
for (int x = 0; x < width; x += step)
{
double z = image->GetPixelZBuffer(x, y);
unsigned short opacity16 = image->GetPixelOpacity(x, y);
double opacity = opacity16 / 65535.0;
double total_ambient = 0;
if (z < 1e19)
{
// printf("SSAO point on object\n");
double x2, y2;
if (perspectiveType == params::perspFishEye)
{
x2 = M_PI * ((double)x / width - 0.5) * aspectRatio;
y2 = M_PI * ((double)y / height - 0.5);
double r = sqrt(x2 * x2 + y2 * y2);
if (r != 0.0)
{
x2 = x2 / r * sin(r * fov) * z;
y2 = y2 / r * sin(r * fov) * z;
}
}
else if (perspectiveType == params::perspEquirectangular)
{
x2 = M_PI * ((double)x / width - 0.5) * aspectRatio;
y2 = M_PI * ((double)y / height - 0.5);
x2 = sin(fov * x2) * cos(fov * y2) * z;
y2 = sin(fov * y2) * z;
}
else
{
x2 = ((double)x / width - 0.5) * aspectRatio;
y2 = ((double)y / height - 0.5);
x2 = x2 * z * fov;
y2 = y2 * z * fov;
}
double ambient = 0;
double angleStep = M_PI * 2.0 / (double)quality;
int maxRandom = 62831 / quality;
double rRandom = 1.0;
if (params->SSAO_random_mode) rRandom = 0.5 + Random(65536) / 65536.0;
for (int angleIndex = 0; angleIndex < quality; angleIndex++)
{
double ca, sa;
if (params->SSAO_random_mode)
{
double angle = angleStep * angleIndex + Random(maxRandom) / 10000.0;
ca = cos(angle);
sa = sin(angle);
}
else
{
//.........这里部分代码省略.........
示例9: CmdBuildCanal
/**
* Build a piece of canal.
* @param tile end tile of stretch-dragging
* @param flags type of operation
* @param p1 start tile of stretch-dragging
* @param p2 waterclass to build. sea and river can only be built in scenario editor
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdBuildCanal(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{
WaterClass wc = Extract<WaterClass, 0, 2>(p2);
if (p1 >= MapSize() || wc == WATER_CLASS_INVALID) return CMD_ERROR;
/* Outside of the editor you can only build canals, not oceans */
if (wc != WATER_CLASS_CANAL && _game_mode != GM_EDITOR) return CMD_ERROR;
TileArea ta(tile, p1);
/* Outside the editor you can only drag canals, and not areas */
if (_game_mode != GM_EDITOR && ta.w != 1 && ta.h != 1) return CMD_ERROR;
CommandCost cost(EXPENSES_CONSTRUCTION);
TILE_AREA_LOOP(tile, ta) {
CommandCost ret;
Slope slope = GetTileSlope(tile);
if (slope != SLOPE_FLAT && (wc != WATER_CLASS_RIVER || !IsInclinedSlope(slope))) {
return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
}
/* can't make water of water! */
if (IsTileType(tile, MP_WATER) && (!IsTileOwner(tile, OWNER_WATER) || wc == WATER_CLASS_SEA)) continue;
bool water = IsWaterTile(tile);
ret = DoCommand(tile, 0, 0, flags | DC_FORCE_CLEAR_TILE, CMD_LANDSCAPE_CLEAR);
if (ret.Failed()) return ret;
if (!water) cost.AddCost(ret);
if (flags & DC_EXEC) {
switch (wc) {
case WATER_CLASS_RIVER:
MakeRiver(tile, Random());
if (_game_mode == GM_EDITOR) {
TileIndex tile2 = tile;
CircularTileSearch(&tile2, 5, RiverModifyDesertZone, NULL);
}
break;
case WATER_CLASS_SEA:
if (TileHeight(tile) == 0) {
MakeSea(tile);
break;
}
/* FALL THROUGH */
default:
MakeCanal(tile, _current_company, Random());
if (Company::IsValidID(_current_company)) {
Company::Get(_current_company)->infrastructure.water++;
DirtyCompanyInfrastructureWindows(_current_company);
}
break;
}
MarkTileDirtyByTile(tile);
MarkCanalsAndRiversAroundDirty(tile);
}
cost.AddCost(_price[PR_BUILD_CANAL]);
}
示例10: Initialize
func Initialize() {
SetR(Random(360));
AddAlienFlareEffect(this(),100);
//AddFireEffect(this(),0,RGB(20,255,30),0,-20);
}
示例11: main
int main(){
int i, j, k, r, index;
int failCount = 0;
int totalCount = 0;
int numPlayers = 2;
int seed = 5555;
int kingCards[10] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, great_hall};
struct gameState G;
FILE* outfile;
FILE* reportfile;
outfile = fopen("unittest4.result", "w");
reportfile = fopen("unittestresult.out", "a");
fprintf(reportfile, "Testing isGameOver, with all combinations of number of provinces left (0-12), and number of other piles at zer0 (0-17).\n");
for(i = 0; i < 13; i++)/*Number of Provinces*/{
for(j = 0; j < 18; j++)/*Number of Piles at zero*/{
memset(&G, 23, sizeof(struct gameState));
r = initializeGame(numPlayers, kingCards, seed, &G);
G.supplyCount[3] = i;
for(k = 0; k < j; k++){
index = floor(Random() * treasure_map);
if(index == 3) k--;
else if(G.supplyCount[index] == 0) k--;
else G.supplyCount[index] = 0;
}
r = isGameOver(&G);
totalCount++;
if(i <= 0){
if(r == 1) fprintf(outfile, "SUCCESS: Correctly found that Province pile is empty. (%d, %d)\n", i, j);
else{
fprintf(outfile, "FAILURE: Did not find that Province pile is empty. (%d, %d)\n", i, j);
failCount++;
}
}
else if(r == 1){
fprintf(outfile, "FAILURE: Incorrectly found that Province pile is empty. (%d, %d)\n", i, j);
failCount++;
}
else fprintf(outfile, "SUCCESS: Did not incorrectly find the Province pile is empty. (%d, %d)\n", i, j);
if(j >= 3 && r > 1){
if(r == 2) fprintf(outfile, "SUCCESS: Correctly found that 3 or more piles are empty. (%d, %d)\n", i, j);
else{
fprintf(outfile, "FAILURE: Did not find that 3 or more piles were empty. (%d, %d)\n", i, j);
failCount++;
}
}
else if(r == 2){
fprintf(outfile, "FAILURE: Incorrectly found that 3 or more piles were empty. (%d, %d)\n", i, j);
failCount++;
}
else fprintf(outfile, "SUCCESS: Did not incorrectly find that 3 or more piles were empty. (%d, %d)\n", i, j);
}
}
fprintf(reportfile, "NUMBER OF COMBINATIONS TESTED: %d\n", totalCount);
fprintf(reportfile, "NUMBER OF FAILED TESTS (2 possible per unique set of inputs): %d\n", failCount);
fclose(outfile);
fclose(reportfile);
return 0;
}
示例12: memcpy
void OcdDecoder::RecursiveDecode(blockID_t blockID) {
uint32_t blockIndex, unsolvedBlockIndex;
uint8_t *solvedBlock; /* Used to point to already known blocks */
uint32_t degree, degree_it;
std::list<blockID_t>::iterator blockID_it;
uint32_t numberOfUnsolvedBlocks;
/* Check if remaining degree is 1, so that we can derive
information from it right now. If not, return immediately. */
if(mBlockRemainingDegreeList[blockID] != 1)
return;
/* Set solution holder to the contents of the checkBlock */
memcpy(mBlockSolution, mCheckBlockCache[blockID], mBlockSize);
/* Sweep all blocks related to this check block, and xor them together.
additionally, look for the block which has not been solved yet,
so that we could know where to commit the solution */
SeedCodec(blockID);
degree = GetDegree();
numberOfUnsolvedBlocks = 0;
for(degree_it = 0; degree_it < degree; degree_it++) {
blockIndex = Random(mNumberOfIntermediateBlocks);
if(mBlockStatus[blockIndex] == BLOCK_SOLVED) {
/* Get block pointer */
GetBlock(mIntermediateBuffer.buffer, blockIndex, &solvedBlock);
BlockXor(mBlockSolution, solvedBlock);
}
else {
numberOfUnsolvedBlocks++;
unsolvedBlockIndex = blockIndex;
}
}
/* At this point, blockSolution contains the solution for the
block at unsolvedBlockIndex. Just commit it */
GetBlock(mIntermediateBuffer.buffer, unsolvedBlockIndex, &solvedBlock);
memcpy(solvedBlock, mBlockSolution, mBlockSize);
/* Now the update stage.
First step: Flag solvedBlockIndex as "BLOCK_SOLVED" and update
numberOfRemainingBlocks */
mBlockStatus[unsolvedBlockIndex] = BLOCK_SOLVED;
mNumberOfRemainingBlocks--;
/* Second step: decrease by one degree all checkBlocks related to
the solved one. */
for(blockID_it = mRelatedBlockIDLists[unsolvedBlockIndex].begin();
blockID_it != mRelatedBlockIDLists[unsolvedBlockIndex].end();
blockID_it++) {
if(mBlockRemainingDegreeList[*blockID_it] > 0)
mBlockRemainingDegreeList[*blockID_it]--;
}
/* Third step: evaluate recursively those blocks which have degree equal to 1 */
for(blockID_it = mRelatedBlockIDLists[unsolvedBlockIndex].begin();
blockID_it != mRelatedBlockIDLists[unsolvedBlockIndex].end();
blockID_it++) {
if(mBlockRemainingDegreeList[*blockID_it] == 1)
RecursiveDecode(*blockID_it);
}
/* Fourth step (optional): clear list of relational blocks */
mRelatedBlockIDLists[unsolvedBlockIndex].clear();
}
示例13: main
int main()
{
int i, j, p, position;
struct gameState state;
SelectStream(2);
PutSeed(3);
for (i = 0; i < 2000; i++)
{
for (j = 0; j < sizeof(struct gameState); j++)
{
((char*)&state)[j] = floor(Random() * 256);
}
p = floor(Random() * 4);
position = floor(Random() * 20);
state.deckCount[p] = floor(Random() * MAX_DECK);
state.discardCount[p] = floor(Random() * MAX_DECK);
state.playedCardCount = floor(Random() * MAX_DECK);
state.handCount[p] = floor(Random() * MAX_HAND);
state.whoseTurn = p;
state.numActions = floor(Random() * 1);
for (j = 0; j < treasure_map; j++)
{
state.supplyCount[j] = floor(Random() * 25);
}
for (j = 0; j < state.handCount[p]; j++)
{
state.hand[p][j] = floor(Random() * MAX_HAND);
}
for (j = 0; j < state.discardCount[p]; j++)
{
state.discard[p][j] = floor(Random() * MAX_DECK);
}
for (j = 0; j < state.deckCount[p]; j++)
{
state.deck[p][j] = floor(Random() * MAX_DECK);
}
for (j = 0; j < state.playedCardCount; j++)
{
state.playedCards[j] = floor(Random() * MAX_DECK);
}
testVillageCard(p, &state, position);
}
printf("No errors found in tests of Village Card \n");
return 0;
}
示例14: HotPotato_Init
//------------HotPotato_Init------------
// Setup hot-potato game mode elements
// Input: none
// Output: none
void HotPotato_Init(void){
HotPotato_SetIt(Random()%4);
}
示例15: gObjAddMonster
BOOL MonsterHerd::AddMonster(int iMonsterType, BOOL bRegen, BOOL bAttackFirst)
{
if ( this->m_bHasInfo == 0 )
{
return false;
}
int iIndex;
BYTE btMapNumber = this->m_iMapNumber;
BYTE cX=0;
BYTE cY=0;
if (this->GetRandomLocation(cX, cY) == FALSE )
{
return false;
}
iIndex = gObjAddMonster(this->m_iMapNumber);
if ( iIndex >= 0 )
{
gObj[iIndex].m_PosNum = -1;
gObj[iIndex].X = cX;
gObj[iIndex].Y = cY;
gObj[iIndex].MapNumber = this->m_iMapNumber;
gObj[iIndex].TX = gObj[iIndex].X;
gObj[iIndex].TY = gObj[iIndex].Y;
gObj[iIndex].m_OldX = gObj[iIndex].X;
gObj[iIndex].m_OldY = gObj[iIndex].Y;
gObj[iIndex].StartX = (BYTE)gObj[iIndex].X;
gObj[iIndex].StartY = (BYTE)gObj[iIndex].Y;
LPMONSTER_ATTRIBUTE iAttr = gMAttr.GetAttr(iMonsterType);
if ( iAttr == NULL )
{
gObjDel(iIndex);
return false;
}
gObj[iIndex].Level = iAttr->m_Level;
gObjSetMonster(iIndex, iMonsterType);
gObj[iIndex].MaxRegenTime = 1000;
gObj[iIndex].Dir = Random(0,7);
gObj[iIndex].m_bIsInMonsterHerd = 1;
gObj[iIndex].m_bIsMonsterAttackFirst = bAttackFirst;
gObj[iIndex].m_lpMonsterHerd = this;
EnterCriticalSection(&this->m_critMonsterHerd);
_MONSTER_HERD_DATA pMonsterData;
pMonsterData.m_iIndex = iIndex;
pMonsterData.m_iType = iMonsterType;
pMonsterData.m_iX = cX;
pMonsterData.m_iY = cY;
pMonsterData.m_bRegen = bRegen;
this->m_mapMonsterHerd.insert( std::pair<int, _MONSTER_HERD_DATA>(iIndex, pMonsterData) );
LeaveCriticalSection(&this->m_critMonsterHerd);
}
else
{
return FALSE;
}
return TRUE;
}