原文地址:http://blog.youkuaiyun.com/aheroofeast/article/details/3907192
首先简单的介绍一个扑克牌洗牌的方法,假设一个数组 poker[52] 中存有一副扑克牌1-52的牌点值,使用一个for循环遍历这个数组,每次循环都生成一个[0,52)之间的随机数RandNum,以RandNum为数组下标,把当前下标对应的值和RandNum对应位置的值交换,循环结束,每个牌都与某个位置交换了一次,这样一副牌就被打乱了。
[cpp]
view plain
copy
- for (int i = 0; i < 52; ++i)
- {
- int RandNum = rand() % 52;
- int tmp = poker[i];
- poker[i] = poker[RandNum];
- poker[RandNum] = tmp;
- }
random_shuffle 的第三个参数,需要的是一个函数对象, 这个函数对象的参数是算法遍历序列时的index,返回值是0-X 之间的一个随机数,这个 X 可由用户来决定。默认的random_shuffle中, 被操作序列的index 与 rand() % N 两个位置的值交换,来达到乱序的目的。
[cpp]
view plain
copy
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <ctime>
- #include <cstdlib>
- using namespace std;
- const int POKER_NUM = 52; //52张扑克牌
- void print_poker(int PokerNum)
- {
- cout << PokerNum << " ";
- }
- class MyRand
- {
- public:
- int operator()(int index)
- {
- return rand() % POKER_NUM;
- }
- };
- int main()
- {
- srand( (unsigned)time(NULL) ); //设置随即数生成器的种子
- vector<int> poker; //一副牌,牌点数从 1 计
- //初始化
- for (int num = 0; num < POKER_NUM; ++num)
- {
- poker.push_back(num+1);
- }
- //用默认随机数洗一遍
- random_shuffle(poker.begin(), poker.end());
- for_each(poker.begin(), poker.end(), print_poker);
- cout << endl;
- //用自定义随机数再洗一遍
- random_shuffle(poker.begin(), poker.end(), MyRand());
- copy(poker.begin(), poker.end(), ostream_iterator<int>(cout, " "));
- cout << endl;
- }