The basic use of random_shuffle()

本文探讨了如何使用C++代码实现字符串和整数向量的随机洗牌,以确保每次运行时结果不同。通过引入`<ctime>`和`<cstdlib>`库,作者展示了如何生成不可预测的随机序列,以实现每次迭代结果的变化。
摘要由CSDN通过智能技术生成

Just look at the codes

#include <iostream>
#include <algorithm>
#include <vector>

int main()
{
    std::vector<std::string> v = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
    std::vector<std::string>::iterator it;
    for (it = v.begin(); it != v.end(); ++it)
    {
        std::cout << *it << " ";
    }
    std::cout << std::endl;
    std::random_shuffle(v.begin(), v.end());
    for (it = v.begin(); it != v.end(); ++it)
    {
        std::cout << *it << " ";
    }
    std::cout << std::endl;
    std::vector<int> v2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::vector<int>::iterator it2;
    for (it2 = v2.begin(); it2 != v2.end(); ++it2)
    {
        std::cout << *it2 << " ";
    }
    std::cout << std::endl;
    std::random_shuffle(v2.begin(), v2.end());
    for (it2 = v2.begin(); it2 != v2.end(); ++it2)
    {
        std::cout << *it2 << " ";
    }
    std::cout << std::endl;
    return 0;
}

Corresponding results

a b c d e f g h i j k l m n o p q r s t u v w x y z
e k l p y q r b g j d w t c a x z s n i f v h m o u
1 2 3 4 5 6 7 8 9 10
9 6 8 4 3 7 1 5 10 2

The above codes cannot achieve the results will change with every iteration of running

The codes to achieve that you can get different results after you run your codes

#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>

int main()
{
    std::vector<std::string> v = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
    std::vector<std::string>::iterator it;
    for (it = v.begin(); it != v.end(); ++it)
    {
        std::cout << *it << " ";
    }
    std::cout << std::endl;
    std::random_shuffle(v.begin(), v.end());
    for (it = v.begin(); it != v.end(); ++it)
    {
        std::cout << *it << " ";
    }
    std::cout << std::endl;
    std::vector<int> v2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::vector<int>::iterator it2;
    for (it2 = v2.begin(); it2 != v2.end(); ++it2)
    {
        std::cout << *it2 << " ";
    }
    std::cout << std::endl;
    std::random_shuffle(v2.begin(), v2.end());
    for (it2 = v2.begin(); it2 != v2.end(); ++it2)
    {
        std::cout << *it2 << " ";
    }
    std::cout << std::endl;
    // make the result of the random_shuffle unpredictable
    srand(time(NULL));
    std::vector<int> v3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::vector<int>::iterator it3;
    for (it3 = v3.begin(); it3 != v3.end(); ++it3)
    {
        std::cout << *it3 << " ";
    }
    std::cout << std::endl;
    std::random_shuffle(v3.begin(), v3.end());
    for (it3 = v3.begin(); it3 != v3.end(); ++it3)
    {
        std::cout << *it3 << " ";
    }
    std::cout << std::endl;
    return 0;
}

The corresponding results:

first_try

a b c d e f g h i j k l m n o p q r s t u v w x y z
e k l p y q r b g j d w t c a x z s n i f v h m o u
1 2 3 4 5 6 7 8 9 10
9 6 8 4 3 7 1 5 10 2
1 2 3 4 5 6 7 8 9 10
3 2 4 8 6 9 1 5 10 7

second_try

a b c d e f g h i j k l m n o p q r s t u v w x y z
e k l p y q r b g j d w t c a x z s n i f v h m o u
1 2 3 4 5 6 7 8 9 10
9 6 8 4 3 7 1 5 10 2
1 2 3 4 5 6 7 8 9 10
8 10 9 4 5 6 1 3 7 2

third_try

a b c d e f g h i j k l m n o p q r s t u v w x y z
e k l p y q r b g j d w t c a x z s n i f v h m o u
1 2 3 4 5 6 7 8 9 10
9 6 8 4 3 7 1 5 10 2
1 2 3 4 5 6 7 8 9 10
7 3 1 10 4 6 9 8 5 2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值