コード例 #1
0
ファイル: util.cpp プロジェクト: Einsyui/RunIdi_3
QString Util::getRandomElement(QVector<QString> list)
{
    int ran = getRandomNum( 0, list.size() - 1 );
    Util::log->log( "Chose random element number " + QString::number( ran ) );
    Util::log->log( "Element: " + list[ran] );
    return list[ ran ];
}
コード例 #2
0
vector<CVector> CellInitHelper::tryGenInitCellNodes(uint initNodeCt) {
	double radius =
			globalConfigVars.getConfigValue("InitCellRadius").toDouble();
	vector<CVector> poss;
	int foundCount = 0;
	double randX, randY;
	while (foundCount < initNodeCt) {
		bool isInCircle = false;
		while (!isInCircle) {
			randX = getRandomNum(-radius, radius);
			randY = getRandomNum(-radius, radius);
			isInCircle = (sqrt(randX * randX + randY * randY) < radius);
		}
		poss.push_back(CVector(randX, randY, 0));
		foundCount++;
	}
	return poss;
}
コード例 #3
0
ファイル: grab.cpp プロジェクト: zhengxiexie/m
int Grab::userGrab(const string& user_id, string& num)
{
	int res=this->getGrabInfo();
	if(res!=0){
		UB_LOG_FATAL( "getGrabInfo failed, [%s:%d]", __FILE__, __LINE__ );
		return -5;
	}
	time_t t;
	t=time(0);
	if(t<stringToTimestamp(_info.start_time)){
		UB_LOG_NOTICE( "_id[%d] does not start[%s]", _id, _info.start_time.c_str() );
		return -1;
	}
	if(t>stringToTimestamp(_info.end_time)){
		UB_LOG_NOTICE( "_id[%d] has already end[%s]", _id, _info.end_time.c_str() );
		return -2;
	}
	if(_info.send_amount>=_info.amount){
		UB_LOG_NOTICE( "_id[%d] has already send end. amount[%d],send_amount[%d]", _id, _info.amount, _info.send_amount );
		return -3;
	}
	res=this->getUserGrab(user_id, num);
	if(res<0){
		UB_LOG_FATAL("getUserGrab failed, [%s:%d]", __FILE__, __LINE__ );
		return -5;
	}else if(res>0){
		UB_LOG_NOTICE( "user[%s] has already get a num[%s] in grab[%d]", user_id.c_str(), num.c_str(), _id );
		return -6;
	}
	long random_num=getRandomNum(100);
	if(random_num<_info.probability){
		res=this->getGrabNum(user_id, num, 1);
		if(res==-5){
			UB_LOG_FATAL("getGrabNum failed, user_id[%s], [%s:%d]", user_id.c_str(), __FILE__, __LINE__ );
			num.clear();
			return -5;
		}else if(res==-3){
			UB_LOG_FATAL("getGrabNum has already send over, id[%d], user_id[%s], [%s:%d]", 
					_id, user_id.c_str(), __FILE__, __LINE__ );
			/*res = this->setSendAmount(_info.amount);*/
			//if(res!=0){
				//UB_LOG_FATAL( "setSendAmount failed, id[%d], [%s:%d]", _id, __FILE__, __LINE__ );
			//}
			/*num.clear();*/
			return -5;
		}else{
			res= this->incrSendAmount();
			if(res!=0){
				UB_LOG_FATAL( "incrSendAmount failed, id[%d], [%s:%d]", _id, __FILE__, __LINE__ );
			}
		}
	}else{
		return -4;
	}
	return 0;
}
コード例 #4
0
ファイル: FrameTable.cpp プロジェクト: jinglundong/OS-Pager
int FrameTable::getRandomNum(FILE *pRandomFile, bool showRandom){
	char tempStr [15];
	if (fscanf(pRandomFile, "%s", tempStr) != EOF) {
		if (showRandom){
			cout<<"?? uses random number: "<<atoi(tempStr)<<"\n";
		}
		return (atoi(tempStr));
	}
	else{
		rewind(pRandomFile);
		return getRandomNum(pRandomFile, showRandom);
	}
}
コード例 #5
0
vector<CVector> CellInitHelper::tryGenInitCellNodes() {
	double radius =
			globalConfigVars.getConfigValue("InitCellRadius").toDouble();
	//int initCellNodeCount =
	//    globalConfigVars.getConfigValue("InitCellNodeCount").toInt();
	// now we need non-uniform initial growth progress.
	int initCellNodeCount =
			globalConfigVars.getConfigValue("InitCellNodeCount").toInt();
	vector<CVector> poss;
	int foundCount = 0;
	double randX, randY;
	while (foundCount < initCellNodeCount) {
		bool isInCircle = false;
		while (!isInCircle) {
			randX = getRandomNum(-radius, radius);
			randY = getRandomNum(-radius, radius);
			isInCircle = (sqrt(randX * randX + randY * randY) < radius);
		}
		poss.push_back(CVector(randX, randY, 0));
		foundCount++;
	}
	return poss;
}
コード例 #6
0
ファイル: grab.cpp プロジェクト: zhengxiexie/m
int Grab::getGrabNum(const string& user_id, string& num, int32_t is_pre)
{
	extern string IP;
	int res;
	string query_sql;
	//DuokooMysql mysql;
    DuokooMysql mysql("grab_mysql");
	if(!mysql.startTransaction()){
		return -5;
	}
	//get num from mysql
	//comment by zhengxie 2013.10.22
	/*query_sql="SELECT num FROM `MCP`.`mcp_content_grab_num` WHERE `is_occupy` = 0 AND `grab_id` = " + toString(_id) + " limit 0, 1";*/
	//UB_LOG_DEBUG("query_sql:[%s]", query_sql.c_str());
	//res=mysql.query(query_sql);
	//if(res<0){
		//UB_LOG_FATAL( "query sql[%s] failed, [%s], [%s:%d]", query_sql.c_str(), mysql.getErrDes().c_str(), __FILE__, __LINE__ );
		//mysql.rollback();
		//return -5;
	//}else if(res==0){
		//UB_LOG_FATAL( "query sql[%s] have no record, [%s:%d]", query_sql.c_str(), __FILE__, __LINE__ );
		//mysql.rollback();
		//return -3;
	//}
	/*num=mysql.getResult(0,0);*/

	/* 2013.10.22 add by zhengxie get num from mysql => get num from redis start */
	redisContext *c;
    uint32_t send_amount(0);
    c=redisConnPool::getInstance()->getConnection();
    if(c==NULL)
	{
        UB_LOG_FATAL( "get redis connection failed, [%s:%d]", __FILE__, __LINE__ );
        return -5;
    }
    redisReply *reply;
	/* 2013.12.03 add by zhengxie add lock */

	string thread_id = DuokooTools::toString(CommonInterface::get_thread_id());
	string random = DuokooTools::toString(getRandomNum(10000000));

	string key = "grab_and_user_id:" + DuokooTools::toString(_id) + "," + user_id;
	string value = IP + "," + thread_id + "," + random;

	redisCommand(c, "SETNX %s %s", key.c_str(), value.c_str());

	LOGD( "[zx]get_value key:%s ", key.c_str() );
    reply = (redisReply*)redisCommand( c, "GET %s", key.c_str() );

    redisConnPool::getInstance()->releaseConnection(c);
    if(reply!=NULL)
	{
        if(reply->type!=REDIS_REPLY_ERROR)
		{
            if(reply->type!=REDIS_REPLY_NIL)
			{
                string temp_value = reply->str;
				freeReplyObject(reply);
				reply = NULL;
				if(strcmp(temp_value.c_str(), value.c_str()))
				{
					LOGA( "[zx] shit, bad user[%s] ", user_id.c_str() );
					return -3;//同一个用户重复抢号
				}
            }
			else
			{
				freeReplyObject(reply);
				reply = NULL;
				return -3;
            }
		}
	}
	else
	{
		LOGA( "[zx] key not set[%s] ", key.c_str() );
		return -5;
	}

	/* 2013.12.03 add by zhengxie add lock */

	c=redisConnPool::getInstance()->getConnection();
    if(c==NULL)
	{
        UB_LOG_FATAL( "get redis connection failed, [%s:%d]", __FILE__, __LINE__ );
        return -5;
    }

	redisCommand(c, "EXPIRE %s %ld", key.c_str(), 10*24*60*60);
    //LOGD( "[zx]SPOP grab_id:%d ", _id );
    reply = (redisReply*)redisCommand( c, "SPOP grab_id:%d", _id );
    redisConnPool::getInstance()->releaseConnection(c);
    if(reply!=NULL)
	{
        if(reply->type!=REDIS_REPLY_ERROR)
		{
            if(reply->type!=REDIS_REPLY_NIL)
			{
                LOGA( "grab_id:[%d], user_id:[%s], grab_num:[%s]", _id, user_id.c_str(), reply->str );
                num=reply->str;
				freeReplyObject(reply);
				reply = NULL;
            }
			else
			{
				LOGD( "[zx] has no grab_nums grab_id[%d] ", _id );
				freeReplyObject(reply);
				reply = NULL;
				return -3;
            }
		}
	}
	else
	{
		LOGD( "[zx] not exist grab_id[%d] ", _id );
		return -5;
	}

	/* 2013.10.22 add by zhengxie get num from mysql => get num from redis end */
	//update num
	query_sql="UPDATE `MCP`.`mcp_content_grab_num` SET `is_occupy` = "+ toString(is_pre) +" WHERE num = '" + num + "' ";
	UB_LOG_DEBUG("query_sql:[%s]", query_sql.c_str());
	res=mysql.query(query_sql);
	if(res<0){
		UB_LOG_FATAL( "query sql[%s] failed, [%s], [%s:%d]", query_sql.c_str(), mysql.getErrDes().c_str(), __FILE__, __LINE__ );
		mysql.rollback();
		return -5;
	}
	//insert user_num
	query_sql="INSERT INTO `MCP`.`mcp_content_user_grab`( `id`, `grab_id`, `user_id`, `grab_time`, `use_time`, `num` )"
		"VALUES( NULL, '"+toString(_id)+"', '"+user_id+"', '"+NOW()+"', '0', '"+num+"' ) ";
	UB_LOG_DEBUG("query_sql:[%s]", query_sql.c_str());
	res=mysql.query(query_sql);
	if(res<0){
		UB_LOG_FATAL( "query sql[%s] failed, [%s], [%s:%d]", query_sql.c_str(), mysql.getErrDes().c_str(), __FILE__, __LINE__ );
		mysql.rollback();
		return -5;
	}
	return mysql.commit()?0:-5;
}
コード例 #7
0
ファイル: Proc.cpp プロジェクト: jinglundong/OS-Pager
void Proc::goToNextRef(int procNum, FILE *pRandomFile, bool showRandom){
	int randomNum = getRandomNum(pRandomFile, showRandom);
	int randomNum2;
	double y = randomNum / (2147483648 + 1.0);
	switch (jobMix){
	case 1:
		curWord += 1;
		break;
	case 2:
		curWord += 1;
		break;
	case 3:
		randomNum2 = getRandomNum(pRandomFile, showRandom);
		curWord = randomNum2;
		break;
	case 4:
		switch (procNum){
		case 0:
			if (y<0.75){
				curWord += 1;
			}
			else{
				curWord -= 5;
			}
			break;
		case 1:
			if ( y <0.75){
				curWord += 1;
			}
			else{
				curWord += 4;
			}
			break;
		case 2:
			if (y < 0.75){
				curWord +=1;
			}
			else{
				if (y <0.875){
					curWord -=5;
				}
				else{
					curWord +=4;
				}
			}
			break;
		case 3:
			if (y < 0.5){
				curWord += 1;
			}
			else{
				if (y < 0.625){
					curWord -= 5;
				}
				else{
					if (y < 0.75){
						curWord += 4;
					}
					else{
						randomNum2 = getRandomNum(pRandomFile, showRandom);
						curWord = randomNum2;
					}
				}
			}
			break;
		}
	}
	curWord = (curWord + processSize)%processSize;
	curNumRef ++;
	if (curNumRef>numRef){
		finishFlag = true;
	}
}