本文整理汇总了C++中RandomInt函数的典型用法代码示例。如果您正苦于以下问题:C++ RandomInt函数的具体用法?C++ RandomInt怎么用?C++ RandomInt使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了RandomInt函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: CAddrInfo
CAddrInfo CAddrMan::Select_(bool newOnly)
{
if (size() == 0)
return CAddrInfo();
if (newOnly && nNew == 0)
return CAddrInfo();
// Use a 50% chance for choosing between tried and new table entries.
if (!newOnly &&
(nTried > 0 && (nNew == 0 || RandomInt(2) == 0))) {
// use a tried node
double fChanceFactor = 1.0;
while (1) {
int nKBucket = RandomInt(ADDRMAN_TRIED_BUCKET_COUNT);
int nKBucketPos = RandomInt(ADDRMAN_BUCKET_SIZE);
while (vvTried[nKBucket][nKBucketPos] == -1) {
nKBucket = (nKBucket + insecure_rand.randbits(ADDRMAN_TRIED_BUCKET_COUNT_LOG2)) % ADDRMAN_TRIED_BUCKET_COUNT;
nKBucketPos = (nKBucketPos + insecure_rand.randbits(ADDRMAN_BUCKET_SIZE_LOG2)) % ADDRMAN_BUCKET_SIZE;
}
int nId = vvTried[nKBucket][nKBucketPos];
assert(mapInfo.count(nId) == 1);
CAddrInfo& info = mapInfo[nId];
if (RandomInt(1 << 30) < fChanceFactor * info.GetChance() * (1 << 30))
return info;
fChanceFactor *= 1.2;
}
} else {
// use a new node
double fChanceFactor = 1.0;
while (1) {
int nUBucket = RandomInt(ADDRMAN_NEW_BUCKET_COUNT);
int nUBucketPos = RandomInt(ADDRMAN_BUCKET_SIZE);
while (vvNew[nUBucket][nUBucketPos] == -1) {
nUBucket = (nUBucket + insecure_rand.randbits(ADDRMAN_NEW_BUCKET_COUNT_LOG2)) % ADDRMAN_NEW_BUCKET_COUNT;
nUBucketPos = (nUBucketPos + insecure_rand.randbits(ADDRMAN_BUCKET_SIZE_LOG2)) % ADDRMAN_BUCKET_SIZE;
}
int nId = vvNew[nUBucket][nUBucketPos];
assert(mapInfo.count(nId) == 1);
CAddrInfo& info = mapInfo[nId];
if (RandomInt(1 << 30) < fChanceFactor * info.GetChance() * (1 << 30))
return info;
fChanceFactor *= 1.2;
}
}
}
示例2: TestStress3
// push_front, rotate the items
void TestStress3(int size)
{
std::cout << "\nTestStress3..." << std::endl;
CS170::List<int> list;
for (int i = 0; i < size; i++)
list.push_front(RandomInt(1, 9));
//std::cout << list;
for (int i = 0; i < size; i++)
{
list.push_back(list.front());
list.pop_front();
//std::cout << list;
}
//std::cout << list;
std::cout << "Items in the list: " << list.size() << std::endl;
std::cout << std::endl;
}
示例3: valiant_torus
void valiant_torus( const Router_gpgpu *r, const Flit *f, int in_channel, OutputSet *outputs, bool inject )
{
int out_port;
int vc_min, vc_max;
outputs->Clear( );
if ( in_channel == 2*gN ) {
f->ph = 1; // Phase 1
f->intm = RandomInt( gNodes - 1 );
}
if ( ( f->ph == 1 ) && ( r->GetID( ) == f->intm ) ) {
f->ph = 2; // Go to phase 2
in_channel = 2*gN; // ensures correct vc selection at the beginning of phase 2
}
if ( f->ph == 1 ) { // In phase 1
dor_next_torus( r->GetID( ), f->intm, in_channel,
&out_port, &f->ring_par, false );
if ( f->ring_par == 0 ) {
vc_min = 0;
vc_max = gNumVCS/4 - 1;
} else {
vc_min = gNumVCS/4;
vc_max = gNumVCS/2 - 1;
}
} else { // In phase 2
dor_next_torus( r->GetID( ), f->dest, in_channel,
&out_port, &f->ring_par, false );
if ( f->ring_par == 0 ) {
vc_min = gNumVCS/2;
vc_max = (3*gNumVCS)/4 - 1;
} else {
vc_min = (3*gNumVCS)/4;
vc_max = gNumVCS - 1;
}
}
outputs->AddRange( out_port, vc_min, vc_max );
}
示例4: RandomInt
void CEnemy::Die(void)
{
int random = RandomInt( 0, 5 );
if( random == 2 )
{
GAME->SlowDownFreakingTimeBro();
CCameraControl::GetInstance()->SetKillCam(true);
}
//do we get a potion?!?
if(rand()%15 == 0)
{
CPlayer::GetInstance()->AcquirePotion();
AUDIO->SFXPlaySound(m_nGetPotionSound);
}
CBaseCharacter::Die();
m_dwDeadTimeStamp = timeGetTime();
}
示例5: distrib
void Population::Mutation(void)
{
unsigned int index;
std::default_random_engine gen;
std::uniform_int_distribution<int> distrib('a', 'z');
std::uniform_real_distribution<double> distrib_d(0,1);
std::vector<char> genes;
for (std::list<Chromosome *>::iterator it = _samples.begin(); it != _samples.end(); ++it)
{
if (distrib_d(gen) <= _mutation_rate)
{
index = RandomInt(0, _gene_size);
genes = (*it)->getGenes();
genes[index] = (char)distrib(gen);
(*it)->setGenes(genes);
}
}
}
示例6: RandomInt
int ChaosRouter::_InputForOutput( int output ) const
{
// return an input that prefers this output
int input;
int offset = RandomInt( _inputs - 1 );
bool match = false;
for ( int i = 0; ( i < _inputs ) && ( !match ); ++i ) {
input = ( i + offset ) % _inputs;
if ( _InputReady( input ) &&
( ! _input_route[input]->OutputEmpty( output ) ) ) {
match = true;
}
}
return match ? input : -1;
}
示例7: RandomInt
void CAddrMan::GetAddr_(std::vector<CAddress>& vAddr)
{
unsigned int nNodes = ADDRMAN_GETADDR_MAX_PCT * vRandom.size() / 100;
if (nNodes > ADDRMAN_GETADDR_MAX)
nNodes = ADDRMAN_GETADDR_MAX;
for (unsigned int n = 0; n < vRandom.size(); n++) {
if (vAddr.size() >= nNodes)
break;
int nRndPos = RandomInt(vRandom.size() - n) + n;
SwapRandom(n, nRndPos);
assert(mapInfo.count(vRandom[n]) == 1);
const CAddrInfo& ai = mapInfo[vRandom[n]];
if (!ai.IsTerrible())
vAddr.push_back(ai);
}
}
示例8: game_loop
void game_loop(int low, int high)
{
int input;
do
{
int number = RandomInt(low, high);
int counter = 1;
/* Initial setup */
do
{
printf("Guess the number I'm thinking of (between %d and %d, "
"0 = quit): ", low, high);
input = get_input(low, high);
} while (input != 0 && input == -1);
while (input != number && input != 0)
{
if (number < input)
{
printf("Too high. Guess again: ");
}
else if (number > input)
{
printf("Too low. Guess again: ");
}
input = get_input(low, high);
++counter;
}
if (input)
{
printf("You guessed the number in %d tries!\n", counter);
printf("Play again? (1=yes, 0=no): ");
scanf("%d", &input);
myfflush();
}
} while (input);
}
示例9: GetRandomCardinalDirection
inline Direction GetRandomCardinalDirection()
{
int randomCardinalDir = RandomInt(0, 3);
switch (randomCardinalDir) {
case 0:
return EAST;
break;
case 1:
return WEST;
break;
case 2:
return SOUTH;
break;
case 3:
return NORTH;
break;
default:
return EAST;
}
}
示例10: CAddrInfo
CAddrInfo CAddrMan::Select_(bool newOnly)
{
if (size() == 0)
return CAddrInfo();
if (newOnly && nNew == 0)
return CAddrInfo();
if (!newOnly && (nTried > 0 && (nNew == 0 || RandomInt(2) == 0))) {
double fChanceFactor = 1.0;
while (1) {
int nKBucket = RandomInt(ADDRMAN_TRIED_BUCKET_COUNT);
int nKBucketPos = RandomInt(ADDRMAN_BUCKET_SIZE);
while (vvTried[nKBucket][nKBucketPos] == -1) {
nKBucket = (nKBucket + insecure_rand()) % ADDRMAN_TRIED_BUCKET_COUNT;
nKBucketPos = (nKBucketPos + insecure_rand()) % ADDRMAN_BUCKET_SIZE;
}
int nId = vvTried[nKBucket][nKBucketPos];
assert(mapInfo.count(nId) == 1);
CAddrInfo& info = mapInfo[nId];
if (RandomInt(1 << 30) < fChanceFactor * info.GetChance() * (1 << 30))
return info;
fChanceFactor *= 1.2;
}
} else {
double fChanceFactor = 1.0;
while (1) {
int nUBucket = RandomInt(ADDRMAN_NEW_BUCKET_COUNT);
int nUBucketPos = RandomInt(ADDRMAN_BUCKET_SIZE);
while (vvNew[nUBucket][nUBucketPos] == -1) {
nUBucket = (nUBucket + insecure_rand()) % ADDRMAN_NEW_BUCKET_COUNT;
nUBucketPos = (nUBucketPos + insecure_rand()) % ADDRMAN_BUCKET_SIZE;
}
int nId = vvNew[nUBucket][nUBucketPos];
assert(mapInfo.count(nId) == 1);
CAddrInfo& info = mapInfo[nId];
if (RandomInt(1 << 30) < fChanceFactor * info.GetChance() * (1 << 30))
return info;
fChanceFactor *= 1.2;
}
}
}
示例11: RandomInt
void TransactionHandler::callRandom(const unsigned int pTimes)
{
//std::lock_guard<std::mutex> locker(m);
size_t s = mTransactions.size();
if (s > 0)
{
for (int i = 0; i < pTimes; i++)
{
//m.lock();
int random = RandomInt((int)mTransactions.size());
Transaction* t = new Transaction(mTransactions[random]);
t->call(); //local variable
delete t;
//m.unlock();
//mTransactions[random].call();
}
}
}
示例12: Find
void CAddrMan::Good_(const CService& addr, int64_t nTime)
{
int nId;
nLastGood = nTime;
CAddrInfo* pinfo = Find(addr, &nId);
if (!pinfo)
return;
CAddrInfo& info = *pinfo;
if (info != addr)
return;
info.nLastSuccess = nTime;
info.nLastTry = nTime;
info.nAttempts = 0;
if (info.fInTried)
return;
int nRnd = RandomInt(ADDRMAN_NEW_BUCKET_COUNT);
int nUBucket = -1;
for (unsigned int n = 0; n < ADDRMAN_NEW_BUCKET_COUNT; n++) {
int nB = (n + nRnd) % ADDRMAN_NEW_BUCKET_COUNT;
int nBpos = info.GetBucketPosition(nKey, true, nB);
if (vvNew[nB][nBpos] == nId) {
nUBucket = nB;
break;
}
}
if (nUBucket == -1)
return;
LogPrint("addrman", "Moving %s to tried\n", addr.ToString());
MakeTried(info, nId);
}
示例13: SelectWeightedSequence
int SelectWeightedSequence( CStudioHdr *pstudiohdr, int activity, int curSequence )
{
VPROF( "SelectWeightedSequence" );
if (! pstudiohdr)
return 0;
if (!pstudiohdr->SequencesAvailable())
return 0;
VerifySequenceIndex( pstudiohdr );
int weighttotal = 0;
int seq = ACTIVITY_NOT_AVAILABLE;
int weight = 0;
for (int i = 0; i < pstudiohdr->GetNumSeq(); i++)
{
int curActivity = GetSequenceActivity( pstudiohdr, i, &weight );
if (curActivity == activity)
{
if ( curSequence == i && weight < 0 )
{
seq = i;
break;
}
weighttotal += iabs(weight);
int randomValue;
if ( IsInPrediction() )
randomValue = SharedRandomInt( "SelectWeightedSequence", 0, weighttotal - 1, i );
else
randomValue = RandomInt( 0, weighttotal - 1 );
if (!weighttotal || randomValue < iabs(weight))
seq = i;
}
}
return seq;
}
示例14: DETOUR_DECL_MEMBER
DETOUR_DECL_MEMBER(ActionResult<CTFBot>, CTFBotMvMEngineerIdle_Update, CTFBot *actor, float dt)
{
SCOPED_INCREMENT(rc_CTFBotMvMEngineerIdle_Update);
TRACE();
static IntervalTimer last_ask;
constexpr float ask_interval = 20.0f;
constexpr float ask_minwait = 3.0f;
if (RandomFloat(0.0f, 1.0f) < (dt / ask_interval)) {
if (last_ask.GetElapsedTime() > ask_minwait) {
last_ask.Start();
switch (RandomInt(0, 2)) {
case 0:
case 1:
actor->SpeakConceptIfAllowed(MP_CONCEPT_PLAYER_TELEPORTERHERE);
break;
case 2:
actor->SpeakConceptIfAllowed(MP_CONCEPT_PLAYER_SENTRYHERE);
break;
}
}
}
auto result = DETOUR_MEMBER_CALL(CTFBotMvMEngineerIdle_Update)(actor, dt);
if (result.transition == ActionTransition::SUSPEND_FOR && result.action != nullptr) {
if (strcmp(result.action->GetName(), "MvMEngineerBuildSentryGun") == 0 && hint_sg != nullptr) {
delete result.action;
result.action = new CTFBotMvMEngineerBuildSGDispenser(hint_sg);
hint_sg = nullptr;
} else if (strcmp(result.action->GetName(), "MvMEngineerBuildTeleportExit") == 0 && hint_te != nullptr) {
delete result.action;
result.action = new CTFBotMvMEngineerBuildTEDispenser(hint_te);
hint_te = nullptr;
}
}
return result;
}
示例15: BeginPlayback
void CASW_Video::OnVideoOver()
{
if ( m_bIsTransition )
{
m_bIsTransition = false;
BeginPlayback( m_nLastTempVideo );
}
else if ( !m_bIsLoopVideo )
{
m_bIsLoopVideo = true;
BeginPlayback( m_nLoopVideo );
}
else if ( m_nNumLoopAlternatives > 0 && RandomFloat() < m_fAlternateChance )
{
PlayTempVideo( m_nLoopVideo + RandomInt( 1, m_nNumLoopAlternatives ) );
}
else
{
bik->SetFrame( GetVideoFaceBIKHandles()->GetBIKHandle( m_nLoopVideo ), 0.0f );
}
}