C++里如何生成10位随机数啊

初墨先生 2014-06-09 04:41:53
用rad函数好像有个范围,我想要的是10位随机数,然后可以转化为string类型的,比如 9876543210,怎么生成这个数啊
...全文
1309 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
帝释天参 2015-12-01
  • 打赏
  • 举报
回复
生成10个109位随机数 #include<iostream> #include<stdlib.h> #include<time.h> long int m=1000000000; //最小 long int n=9999999999; //最大 void main() { srand((int)time(0)); for(int i=0; i<10; i++) { printf("%d\n",rand()%(n-m+1)+m); } getchar(); getchar(); }
初墨先生 2014-06-09
  • 打赏
  • 举报
回复
引用 12 楼 zcdabing 的回复:
[quote=引用 9 楼 NeVer_MoEe 的回复:] [quote=引用 8 楼 zcdabing 的回复:]

	char arr[10];
	time_t t1;
	int num;
	srand((unsigned int) time(&t1));
	for( int i = 0; i!=10;++i)
	{
		num = rand()%10;
		arr[i] = '0'+num;
	}

	string s;
	for(int i=0;i!=10;++i)
	{
		s+=arr[i];
	}
	cout<<s;
参考楼上这么也行
很多地方看不懂啊,没理解[/quote] 1设定随机种子点 2定义10位字符数组,存放10位随机数字 3产生10个个位随机数,并保存在字符数组中 4产生字符串[/quote] 这儿可以产生了,虽然很多代码没学,不懂,但是谢谢!
zcdabing 2014-06-09
  • 打赏
  • 举报
回复
引用 9 楼 NeVer_MoEe 的回复:
[quote=引用 8 楼 zcdabing 的回复:]

	char arr[10];
	time_t t1;
	int num;
	srand((unsigned int) time(&t1));
	for( int i = 0; i!=10;++i)
	{
		num = rand()%10;
		arr[i] = '0'+num;
	}

	string s;
	for(int i=0;i!=10;++i)
	{
		s+=arr[i];
	}
	cout<<s;
参考楼上这么也行
很多地方看不懂啊,没理解[/quote] 1设定随机种子点 2定义10位字符数组,存放10位随机数字 3产生10个个位随机数,并保存在字符数组中 4产生字符串
hjl0508 2014-06-09
  • 打赏
  • 举报
回复
引用 6 楼 NeVer_MoEe 的回复:
[quote=引用 3 楼 hjl0508 的回复:]

long int m=1000000000;//最小的十位数
long int n=9999999999;//最大的十位数
long int result;
result=m+rand()%(n-m+1);
看看行不
编译都没过。。。[/quote]
#include <iostream>
using namespace std;
#include <string>
#include <time.h>
int main()
{
	 srand((int)time(NULL));
	 long int m=1000000000;//最小的十位数
	 long int n=9999999999;//最大的十位数
	 long int result;
	 result=m+rand()%(n-m+1);	 	
	 char s[256];
	 sprintf(s,"%ld",result);
	 string s1=s;
	 cout<<s1<<endl;
	 system("pause");
	 return 0;
}
你试试这个编译通不过?我就是将主要的代码写了一下,你要是直接拿我上面的代码什么都不加去编译肯定通不过啊。。。
Pandorym 2014-06-09
  • 打赏
  • 举报
回复
引用 7 楼 NeVer_MoEe 的回复:
这是我自己写的,但每次生成的都是10个相同的数字,比如1111111111
random函数是随机一个数,然后你再次random的时候还是产生那个数。 要配合randomize();来用。
初墨先生 2014-06-09
  • 打赏
  • 举报
回复
引用 8 楼 zcdabing 的回复:

	char arr[10];
	time_t t1;
	int num;
	srand((unsigned int) time(&t1));
	for( int i = 0; i!=10;++i)
	{
		num = rand()%10;
		arr[i] = '0'+num;
	}

	string s;
	for(int i=0;i!=10;++i)
	{
		s+=arr[i];
	}
	cout<<s;
参考楼上这么也行
很多地方看不懂啊,没理解
zcdabing 2014-06-09
  • 打赏
  • 举报
回复

	char arr[10];
	time_t t1;
	int num;
	srand((unsigned int) time(&t1));
	for( int i = 0; i!=10;++i)
	{
		num = rand()%10;
		arr[i] = '0'+num;
	}

	string s;
	for(int i=0;i!=10;++i)
	{
		s+=arr[i];
	}
	cout<<s;
参考楼上这么也行
初墨先生 2014-06-09
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<iostream>
#include <strstream>
#include <string>
#include<time.h>
#define random(x)(rand()%x)
using namespace std;
int main()
{
    int i,a;
    string x,q;
    for(i=0;i<10;i++)
    {
        srand((int)time(0));
        a=random(9);
        strstream ss;
        ss<<a;
        ss>>q;
        x=x+q;
    }
    cout<<x;
	return 0;
}
这是我自己写的,但每次生成的都是10个相同的数字,比如1111111111
初墨先生 2014-06-09
  • 打赏
  • 举报
回复
引用 3 楼 hjl0508 的回复:

long int m=1000000000;//最小的十位数
long int n=9999999999;//最大的十位数
long int result;
result=m+rand()%(n-m+1);
看看行不
编译都没过。。。
zcdabing 2014-06-09
  • 打赏
  • 举报
回复

	time_t t1;
	srand((unsigned int) time (&t1) );
	int num = rand();
	if(num/100000000)
		num = num%1000000000;
	else
		num += 1000000000;
不知道这么算不算随机
Pandorym 2014-06-09
  • 打赏
  • 举报
回复
int set[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },  
num[5],
Number = 10;
string str = “”;
for (int i = 0; i < 9; i++)
{
    Data = rand() % Number;
    str[i] = set[Data] + 48;
    for (int r = Data; r < Number; r++)
        set[r] = set[r + 1];
    Number--;
}
str[9] = set[0]
没运行过,不知对错。
hjl0508 2014-06-09
  • 打赏
  • 举报
回复

long int m=1000000000;//最小的十位数
long int n=9999999999;//最大的十位数
long int result;
result=m+rand()%(n-m+1);
看看行不
初墨先生 2014-06-09
  • 打赏
  • 举报
回复
引用 1 楼 u013291805 的回复:
抽牌九次,然后集中到字符串。
能不能给点具体代码啊
Pandorym 2014-06-09
  • 打赏
  • 举报
回复
抽牌九次,然后集中到字符串。

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧