cpp: random
// 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, string choices[]);
vector<string> randomizes(int choiceNumber, std::list<std::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 [string choices[]] 输入参数 字符串数组,需要从中抽取的数组
*/
vector<string> RandomlySampled::randomize(int choiceNumber, string choices[])
{
srand(time(0));
int number = sizeof(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::randomizes(int choiceNumber, std::list<std::string> choices)
{
srand(time(0));
int number = choices.size();
vector<string> random;
for (int i = 0; i < choiceNumber; i++) //1是一个,3是三个,需要抽取的数量
{
std::list<std::string>::iterator du = choices.begin();
// random.push_back(choices[rand() % number]); //数组的个数
//1 ok
std::advance(du, rand() % number);
string ss = *du;
//2 也可以
//auto du1 = std::next(choices.begin(), rand() % number);
//ss = *du1;
random.push_back(ss);
}
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;
}
}
/**
* @brief 抽取指定字符串数组的字符
* @param [long] 输入参数 抽几个人中奖
*/
void Geovin::DisplayRomd(int number)
{
srand(time(0));
std::list<std::string> choices = { "涂聚文","刘杰","江山","徐工","李四","王五","张三","赵二"};
RandomlySampled bo;
bo.randomizes(number,choices);
//auto
vector<string> randomResult = bo.randomizes(number,choices);
for (const auto& result : randomResult) {
cout <<"中奖人姓名:"<< result << endl;
}
}
/**
* @brief 抽取指定字符串数组的字符
* @param [long] 输入参数 抽几个人中奖
* @param [list<string>] 输入参数 字符串数组,需要从中抽取的数组
*/
void Geovin::DisplayRomd(int number, std::list<std::string> choices)
{
srand(time(0));
//std::list<std::string> choices = { "涂聚文","刘杰","江山","徐工","李四","王五","张三","赵二" };
RandomlySampled bo;
bo.randomizes(number, choices);
//auto
vector<string> randomResult = bo.randomizes(number, choices);
for (const auto& result : randomResult) {
cout << "中奖人姓名:" << result << endl;
}
}
调用:
//随机抽取字符串
//geovin.DisplayRomd();
//geovin.DisplayRomd(1);
std::list<std::string> choices = { "涂聚文","刘杰","江山","徐工","李四","王五","张三","赵二" };
geovin.DisplayRomd(1, choices);
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
浙公网安备 33010602011771号