cpp: random

发布时间 2023-04-09 15:36:43作者: ®Geovin Du Dream Park™

 

// RandomlySampled.h : 此文件包含 "RandomlySampled" 类。十个常用排序算法 C++ 11
// 2023年4月9日 涂聚文 Geovin Du edit.



#pragma once
#ifndef RANDOMLYSAMPLED_H 
#define RANDOMLYSAMPLED_H 

#include <iostream> 
#include <string> 
#include <vector> 
#include <memory> 
#include <iostream> 
#include <string> 
#include <ctime> 
#include <cstdlib> 
#include <list>




using namespace std;


namespace geovindu
{
    /**
    *@brief 指定字符组数组,指定抽取一个或几个
    *
    */
    class RandomlySampled
    {

    private:
        string bundles[3];
        const string choices[6];
        string random;

    public:

        RandomlySampled();

        string getBundles()
        {
            return bundles[3];
        }
        void setBundles(string b);

        vector<string> randomize();

        vector<string> randomize(int choiceNumber);

        vector<string> randomize(int choiceNumber, list<string> choices);
    };


    RandomlySampled::RandomlySampled()
        :choices{ "" }, bundles{ "" }
    {

    }
    /**
    * @brief 指定字符组数组,指定抽取一个或几个
    *
    */
    vector<string> RandomlySampled::randomize()
    {
        srand(time(0));
        string choices[] = { "Broccoli", "SiboDu", "Kiwi", "Kale", "Toma","GeovinDu" };
        int number = size(choices);
        vector<string> random;
        for (int i = 0; i < 1; i++) //1是一个,3是三个,需要抽取的数量
        {
            random.push_back(choices[rand() % number]); //数组的个数
        }
        return random;
    }

    /**
    * @brief 指定字符组数组,指定抽取一个或几个
    * @param [int] 输入参数
    */
    vector<string> RandomlySampled::randomize(int choiceNumber)
    {
        srand(time(0));
        string choices[] = { "Broccoli", "SiboDu", "Kiwi", "Kale", "Toma","GeovinDu" };
        int number = size(choices);
        vector<string> random;
        for (int i = 0; i < choiceNumber; i++) //1是一个,3是三个,需要抽取的数量
        {
            random.push_back(choices[rand() % number]); //数组的个数
        }
        return random;
    }

    /**
    * @brief 指定字符组数组,指定抽取一个或几个
    * @param [int] 输入参数 指定抽取的几个个数
    * @param [list<string>] 输入参数 字符串数组,需要从中抽取的数组
    */
    vector<string> RandomlySampled::randomize(int choiceNumber, list<string> choices)
    {
        srand(time(0));       
        int number = size(choices);
        vector<string> random;
        for (int i = 0; i < choiceNumber; i++) //1是一个,3是三个,需要抽取的数量
        {
            random.push_back(choices[rand() % number]); //数组的个数
        }
        return random;
    }
};


#endif

  

调用:

	/**
	 # @brief 抽取指定字符串数组的字符
	*/
	void Geovin::DisplayRomd()
	{
		srand(time(0));

		RandomlySampled bo;
		bo.randomize();
		//auto
	   	vector<string> randomResult = bo.randomize();
		for (const auto& result : randomResult) {
			cout << result << endl;
		}
	}