我是c ++的新手,我正在编写一个用于生成随机电话号码的程序。但该程序给出了相同的随机数。 我也用过
srand (time (null));
和
srand (time (0));
我的代码如下
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int randomno (int array [], int size)
{
array [0]=rand()%3+7;
for (int i=1; i<size; i++)
{
array [i]=rand()%10;
}
int phoneno=array[size];
return phoneno;
}
int main ()
{
srand (time (NULL));
int no[10];
cout << "Random phone no is " << randomno (no, 10);
}
此程序应以格式显示数字(9或8或7,然后9位数)。 下面的程序运行良好,可以让你知道我想做什么。
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main ()
{
srand (time (NULL));
cout << "The Random phone No. is ";
int d1, d2, d3, d4, d5, d6, d7, d8, d9, d10;
d1=rand()%3+7;
d2=rand()%10;
d3=rand()%10;
d4=rand()%10;
d5=rand()%10;
d6=rand()%10;
d7=rand()%10;
d8=rand()%10;
d9=rand()%10;
d10=rand()%10;
cout << d1<<d2<<d3<<d4<<d5<<d6<<d7<<d8<<d9<<d10;
}
但后来我决定使用数组和函数。
帮帮我。
答案 0 :(得分:2)
array[size]
超出范围,因此您不得访问(读取或写入)它。
您必须单独打印每个元素才能获得所需内容。
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
void randomno (int array [], int size)
{
array [0]=rand()%3+7;
for (int i=1; i<size; i++)
{
array [i]=rand()%10;
}
}
int main ()
{
srand (time (NULL));
int no[10];
randomno (no, 10);
cout << "Random phone no is ";
for (int i = 0; i < 10; i++) cout << no[i];
}
答案 1 :(得分:2)
您的问题如下:
int randomno(...){...}
应该是
long long randomno(...){...}
int phoneno=array[size];
应该是
long long phoneno = array[9]; // you cannot do it for generic size of array because long has its boundaries
long long temp = 10;
for( int i = 1; i < 10; ++i )
{
phoneno += (array[9 - i] * temp );
temp *= 10;
}
您可以在下面找到我尝试使用c++11 <random>
#include<random>
#include<iostream>
#include<unordered_set>
int main()
{
unsigned number_of_numbers = 10;
std::random_device rd;
std::mt19937 gen( rd() ); // https://en.wikipedia.org/wiki/Mersenne_Twister
std::uniform_int_distribution<> dis( -1000000000,
1999999999 );
std::unordered_set< long long > numbers;
while( numbers.size() != number_of_numbers )
numbers.insert( dis( gen ) + 8000000000 );
for( auto & i : numbers )
std::cout << i << std::endl;
}
产生
9787794699
7356007215
8081593821
7780007650
9167309970
7172218239
8124995716
7281138623
7185218656
8615788448
答案 2 :(得分:0)
函数randomno
采用int
s的数组;它会适当地设置所有值,然后返回array[size]
,这不在数组的末尾。充其量这将产生不可预测的价值。摆脱return语句,并在main
中查看函数设置的十个值。
答案 3 :(得分:0)
int phoneno = array [size]中的数组永远不会设置 - 您为数组位置0 ..(size-1)指定值,因为for条件是i
答案 4 :(得分:0)
您的代码:
int randomno (int array [], int size) {
array [0]=rand()%3+7;
for (int i=1; i<size; i++) { array [i]=rand()%10; }
int phoneno=array[size];
return phoneno;
}
有两个问题。您无法复制像int phoneno=array[size];
这样的数组,也无法从函数返回数组。 return phoneno
返回一个非常无用的垃圾值,与您放入数组的随机值几乎没有关系。
我建议你使用矢量:
std::vector<int> randomno (int size) {
std::vector<int> result;
result.push_back(rand()%3+7);
for (int i=1; i<size; i++) { result.push_back(rand()%10); }
return return result;
}
然后像这样打印:
std::vector<int> phoneno = randomno(10);
for (auto i : phoneno) { std::cout << i; }
std::cout << std::endl;
然而,正如其他人在答案中指出的那样,如果你想要随机数字,你应该看一下<random>
中的rng,rand()
有严重的问题,一旦你需要好的话就不应该使用质量随机数。
答案 5 :(得分:0)
你可以做得更简单:
return (long long)(rand()*((long long)9999999999L-7000000000L+1)+7000000000L);
E&安培; OE