随机获取0~9 不重复的四个数字

#include<iostream>
#include<string>
#include<algorithm>
#include<ctime>
using namespace std;

 

string Random_Result() {
    char temp[] = "1234567890";
    random_shuffle(temp, temp + 10);
    //random_shuffle 用于打乱字符数组
    //temp指向第0个位置
    //temp+10 指向字符串的结束符'0'
    temp[4] = 0; //把字符数组的第五个位置用数字0替换,因为整数0 是字符串结束标志,或者改为'\0'也可以
    return temp; //返回temp前四个字符
}

 

 


int main() {

    srand(unsigned(time(0)));
    string res_String = Random_Result();
    cout << res_String << endl;

 

}

posted @   江南王小帅  阅读(450)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2021-07-06 指向指针的指针变量
2021-07-06 获取数组长度的两种方法
2021-07-06 不同类型数组对应输出
2021-07-06 字符型数组和指针变量
2021-07-06 指针的运算
2021-07-06 利用下标变量显示数组a&利用指针变量显示数组a
点击右上角即可分享
微信分享提示