unsigned int seed = ((int)time(NULL));
srand(seed);
double _rand = static_cast
(rand());
double temp = _rand / static_cast(RAND_MAX);
return static_cast(temp * max);
int main(int argc, char** argv)
// test int
int a[9] = { 100, 200, 300, 400, 500, 600, 700, 800, 900};
/*CSelfGenerator sg;*/
std::random_shuffle(a, a + 9/*, sg*/);
for (int i = 0; i < 9; i++){
cout << a[i] << " ";
cout << endl;
// test float
float _float[N] = { 0.0123, 0.0234, 0.0345, 0.0456, 0.0567 };
std::random_shuffle(_float, _float + N);
for (int i = 0; i < N; i++){
cout << std::fixed << _float[i] << " ";//fixed就是用一般的方式输出浮点数,而不是科学计数法
cout << endl;
// test double
double _double[N] = { 1.2E-2, 2.3E-2, 3.4E-2, 4.5E-2, 5.6E-2 };
std::random_shuffle(_double, _double + N);
for (int i = 0; i < N; i++){
cout << std::fixed << _double[i] << " ";
cout << endl;
// test string
vector strArray;
strArray.push_back("aa");
strArray.push_back("bb");
strArray.push_back("cc");
strArray.push_back("dd");
strArray.push_back("ee");
strArray.push_back("ff");
std::random_shuffle(strArray.begin(), strArray.end());
for(int j = 0; j < strArray.size(); j++){
cout << strArray[j].c_str() << " ";
cout< intArray;
for(int i=0; i<=99; i++){
intArray.push_back(i);
random_shuffle(intArray.begin(), intArray.end()); // 0~99的随机生成
for(int i=0; i<=99; i++){
cout << intArray[i] << " ";
cout<
这里面有个问题:除了最后随机生成99个随机数的顺序被打乱了,其余的每次运行后,顺序都是一样的!何解?
有了解的朋友,如果懂的,麻烦留个言~~
洗牌函数的含义就是将数据打乱顺序,测试代码如下:#include &lt;iostream&gt;#include &lt;algorithm&gt;#include &lt;vector&gt;#include &lt;string&gt;#include &lt;random&gt; // std::default_random_engine#include &lt;time....
template <class
Random
AccessIterator>
void
random
_
shuffle
(
Random
AccessIterator first,
Random
AccessIterator last);
template <class
Random
AccessIterator, class
Random
NumberGenerator>
void
random
_
shuffle
(
Random
AccessIterator first,
Random
Ac
random
−
shuffle
算法\color{blue}
random
-
shuffle
算法
random
−
shuffle
算法
在
STL
中,
函数
random
_
shuffle
()用来对一个元素序列进行随机排序。
函数
原型如下:
template<class
Random
AccessIterator>
void
random
_
shuffle
(
Random
AccessIterator _...
C++中除了使用
rand
和s
rand
函数
产生随机数,
STL
中提供了一个更便捷的方法,即
random
_
shuffle
函数
。
这篇博客主要总结
一下
random
_
shuffle
()的用法,
rand
和s
rand
函数
可以参考我的另一篇博客:
C++随机数:
rand
和s
rand
函数
总结
https://blog.csdn.net/m0_49070560/article/details/108557117.
接下来我们说
random
_
shuffle
()。
random
_
shuffle
()
函数
原型如下:
template
STL
中的
函数
random
_
shuffle
()用来对一个元素序列进行重新排序(随机的)
s
rand
(time(0)); 这个是设置时间种子,加了这句才能保证每次都是随机的,不然就不太随机。
#include<bits/stdc++.h>
using namespace std;
int main()
s
rand
(time(0));
int a[5] = {5,2,2,4,1};
random
_
shuffle
.
//
STL
中的
函数
random
_
shuffle
()用来对一个元素序列进行重新排序(随机的),
函数
原型如下:
/* template <class
Random
AccessIterator>
void
random
_
shuffle
(
Random
AccessIterator _First, //指向序列首元素的迭代器
Random
Access...
random
_
shuffle
是C++
STL
中的一个
函数
,用于将一个序列进行随机排序,也叫
洗牌
算法。
在实际开发中,使用
random
_
shuffle
可以很方便地对一个数组、vector等容器进行随机排序,提高程序的随机性和变化性,使得结果更加具有随机性。
但是需要注意的是,由于
random
_
shuffle
的结果受到随机数种子的影响,因此在使用时一定要加上随机数种子,以保证每次的随机排序结果都不同。可以使用s
rand
函数
设置随机数种子,也可以使用
random
_device获取真正随机的种子。
此外,对于需要保留原始序列的情况,建议先对原始序列进行复制,再对复制的序列进行随机排序,以免影响原始序列的顺序。
综上所述,
random
_
shuffle
洗牌
算法应用广泛,但要注意加上随机数种子。