65,186
社区成员




/*************************************************************************
> File Name: Pass_ball.h
> Author: mz
> Mail:xx@qq.com
> Created Time: Wed 12 Aug 2015 09:20:22 AM CST
************************************************************************/
#ifndef GAME_Pass_ball_h
#define GAME_Pass_ball_h
#include <cstring>
#include <iostream> // for debug
namespace GAME
{
// 枚举类型声明
typedef enum{ LEFT , RIGHT } Choice;
// 类声明
class Pass_ball
{
public:
Pass_ball() : _index( 0 ) { init() ; ::memset( _arr , 0 , sizeof( _arr ) ); }
void run(); // 进行模拟
private:
static const int _round = 100000; // 模拟轮数
static const int _num_per_round = 10 ; // 每一轮传递次数
static const int _total = 5; // 传球人数
int _index; // 随机数表下标
char _choice_table[ _num_per_round * _round ]; // 随机数表
char _arr[ _total ]; // 传球队列
void init(); // 初始化随机数表
Choice get_choice(); // 获得向左或者向右传递的选择
int nrand( int n ); // 生成[0,n)之间的一个随机数
};// Pass_ball
}// GAME
/*************************************************************************
> File Name: Pass_ball.cpp
> Author: mz
> Mail:xx@qq.com
> Created Time: Wed 12 Aug 2015 09:25:40 AM CST
************************************************************************/
#include "Pass_ball.h"
#include <stdexcept>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <iomanip>
using std::domain_error; using std::cerr; using std::endl;
using std::cout; using std::setprecision;
using std::setiosflags; using std::ios_base;
namespace GAME
{
void Pass_ball::run()
{
int count = 0;
// 模拟round次传球
for( int cnt = 0 ; cnt != _round ; ++cnt )
{
int index = 0;
::memset( _arr , 0 , sizeof( _arr ) );
// 开始传球
for( int idx = 0 ; idx != _num_per_round ; ++idx )
{
_arr[ index ] = 0;
Choice choice = get_choice();
if( choice == LEFT )
{
index = ( index - 1 + _total ) % _total;
}
else
{
index = ( index + 1 ) % _total;
}
_arr[ index ] = 1;
}
if( 1 == _arr[ 0 ] ) ++count;
}
// 计算结果
double r = 1.0 * count / _round;
cout << setiosflags( ios_base::fixed ) << setprecision( 6 )
<< "ratio : " << r << endl;
}
void Pass_ball::init()
{
try
{
// 初始化随机数表
for( int i = 0 ; i < ( _num_per_round * _round ) ; ++i )
{
char x = (char)nrand( 2 );
_choice_table[ i ] = x;
}
}
catch( domain_error e )
{
cerr << e.what() << endl;
}
}
Choice Pass_ball::get_choice()
{
char x = _choice_table[ _index++ ];
if( 0 == x ) return LEFT;
else return RIGHT;
}
int Pass_ball::nrand( int n )
{
// 参数检查
if( n <= 0 || n > RAND_MAX )
{
throw domain_error( "Argument to nrand is invalid!" );
}
const int bucket_size = RAND_MAX / n;
int r = 0;
do r = rand() / bucket_size;
while( r >= n );
return r;
}
}//GAME
/*************************************************************************
> File Name: main.cpp
> Author: mz
> Mail:xx@qq.com
> Created Time: Wed 12 Aug 2015 11:36:28 AM CST
************************************************************************/
#include "Pass_ball.h"
using GAME::Pass_ball;
int main( void )
{
Pass_ball pb;
pb.run();
return 0;
}
#endif
Pass_ball pb;
int main( void )
{
//移出 Pass_ball pb;
pb.run();
return 0;
}