首页 > 其他分享 >cpp: random

cpp: random

时间:2023-04-09 15:46:46浏览次数:31  
标签:RandomlySampled int random choices vector cpp include

 

// 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;
		}
	}

  

标签:RandomlySampled,int,random,choices,vector,cpp,include
From: https://www.cnblogs.com/geovindu/p/17300389.html

相关文章

  • (C#)Random实现随机点名
    namespaceWindowsFormsApp3{publicpartialclassForm1:Form{//实例化字符串,设置字符串长度与内容string[]student=newstring[7]{"张三","李四","王五","赵六","hello","world","helloworl......
  • cpp shared_future
    #include<chrono>#include<ctime>#include<future>#include<iomainp>#include<iostream>#include<sstream>#include<uuid/uuid.h>std::stringget_time_now(){std::chrono::time_point<std::chrono::high_......
  • cpp: sort Algorithmic
      //TenSortAlgorithms.h:此文件包含"TenSortAlgotrthms"类。十个常用排序算法C++14//2023年4月5日涂聚文GeovinDuedit.#ifndefTENSORTALGORITHMS_H#defineTENSORTALGORITHMS_H#include<vector>//#includedirective#include<string>#include&l......
  • cpp generate random array then sort by quick sort
    #include<chrono>#include<ctime>#include<iomainp>#include<iostream>#include<random>#include<sstream>std::stringget_time_now(){std::chrono::time_point<std::chrono::high_resolution_clock>now=st......
  • yaml-cpp YAML格式处理库的介绍和使用(面向业务编程-文件格式处理)
    yaml-cppYAML格式处理库的介绍和使用(面向业务编程-文件格式处理)YAML格式介绍YAML的格式介绍,有关ini、json和xml或许很多人已经很了解了,但是关于YAML,还有许多人不了解。YAML被设计成更适合人类阅读(我想正因为如此,所以相对来说更灵活,就导致到使用的时候很多人会觉得它看起来并不......
  • JsonCpp JSON格式处理库的介绍和使用(面向业务编程-文件格式处理)
    JsonCppJSON格式处理库的介绍和使用(面向业务编程-文件格式处理)介绍JSON是一种轻量级的数据交换格式,它是一种键值对的集合。它的值可以是数字、字符串、布尔值、序列。想知道更多有关JSON格式的介绍,可以到JSON的官网json.org学习JsonCpp是一个c++库,允许对JSON值进行操作,包括......
  • cpp: create class
    PigInfo.h#ifndefPIGINFO_H#definePIGINFO_H#include<iostream>#include<string.h>#include<math.h>usingnamespacestd;/*实体类https://learn.microsoft.com/zh-cn/cpp/standard-library/cpp-standard-library-header-files?view=msvc-170......
  • 随机生成工具 RandomValueUtil
    packageentity;/*****@Author:www.itheima.com*@Description:itheima*****/publicclassRandomValueUtil{publicstaticStringbase="abcdefghijklmnopqrstuvwxyz0123456789";privatestaticStringfirstName="赵钱孙李周吴郑王冯陈褚卫蒋沈......
  • Chisel3 使用 DPI-C,发现在 Chisel 环境下 printf 没问题,但是 set_pc 死活传不到 cpp
    大概率是因为你使用了SignExt之类的封装这类封装只会把”值“传给DPI-C,而不会把线连给DPIC,即,传过去的是调用set_pc时的值,而不是引用这样会造成CPP获取不了相应线路的指针 如下图     这些也是错的......
  • Linux环境下使用jsoncpp
    目录1.下载jsoncpp2.生成静态库libjsoncpp.a3.复制相关文件至/usr/local下(方便编程)4.CMakeList.txt编写(需要新增的)1.下载jsoncpp-->https://github.com/open-so......