cpp: random
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | // 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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | /** # @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; } } |
调用:
1 2 3 4 5 6 7 | //随机抽取字符串 //geovin.DisplayRomd(); //geovin.DisplayRomd(1); std::list<std::string> choices = { "涂聚文" , "刘杰" , "江山" , "徐工" , "李四" , "王五" , "张三" , "赵二" }; geovin.DisplayRomd(1, choices); |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
Cpp programming
标签:
随机数
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2015-04-09 csharp: datatable get Column datatype or Column Name