[英]Generating Unique Random Numbers in Arrays C++
I have coded an array with 3 random integers inside of it. 我已经在其中编码了3个随机整数的数组。 The key is though that I want the 3 random integers to be different than each other (unique random numbers). 关键是,尽管我希望3个随机整数彼此不同(唯一的随机数)。 My problem is that even when the numbers are unique, I still get a 'bad' reading from them. 我的问题是,即使数字是唯一的,我仍然会从中得到“不好的”读数。 I seeded my random numbers with time(NULL) and because of this I put a Sleep(x) function in between each declaration to increase variety in numbers. 我用时间(NULL)播种随机数,因此,我在每个声明之间放置了一个Sleep(x)函数以增加数字的多样性。 The following code is all of my code inside my main() function. 以下代码是main()函数中的所有代码。 For testing purposes, I didn't include a break statement in my code so I could test the program over and over. 出于测试目的,我没有在代码中包含break语句,因此可以反复测试程序。
srand((unsigned)time(NULL));
while(true)
{
//Generate 3 numbers
int a = rand() % 7 + 1;
Sleep(1000);
int b = rand() % 8 + 1;
Sleep(1000);
int c = rand() % 9 + 1;
int array[3] = { a , b , c };
//Check the numbers to make sure none of them equal each other
if( (array[a] == array[b]) || (array[a] == array[c]) || (array[b] == array[c]) )
{
//Print all numbers
for(int x = 0; x < 3; x++)
cout << array[x] << endl;
cout << "bad" << endl;
system("pause");
system("cls");
}
else
{
//Print all numbers
for(int x = 0; x < 3; x++)
cout << array[x] << endl;
cout << "good" << endl;
system("pause");
system("cls");
}
}
system("pause");
return 0;
The problem with the current check is it checks at the indices represented by the random values, and not the random values themselves, which are the first 3 elements. 当前检查的问题是它检查由随机值表示的索引 ,而不是前三个元素的随机值本身。
Simply replace 只需更换
if( (array[a] == array[b]) || (array[a] == array[c]) || (array[b] == array[c]) )
with 与
if( (array[0] == array[1]) || (array[0] == array[2]) || (array[1] == array[2]) )
or just 要不就
if(a == b || a == c || b == c)
It appears you're using Sleep
, a Windows-specific function that has nothing to do with the C library. 似乎您正在使用Sleep
,这是Windows特定的功能,与C库无关。 srand
affects the sequence returned by rand()
which is repeatable if srand()
is given the same seed. srand
影响rand()
返回的序列,如果给srand()
相同的种子,则该序列是可重复的。
Secondly, the range of random numbers that you are storing in a
, b
and c
can possibly cause out of bounds array access in this line here: 其次,您存储在a
, b
和c
的随机数范围可能会导致在此行中超出范围的数组访问:
if( (array[a] == array[b]) || (array[a] == array[c]) || (array[b] == array[c]) )
array
only has 3 elements, yet the values in a
, b
and c
can be higher than that. array
只有3个元素,但是a
, b
和c
值可以更高。
Since you are using C++, consider taking advantage of the C++ standard library. 由于您使用的是C ++,因此请考虑利用C ++标准库。
First create a vector of a specified size, and use std::iota
to fill it with values in the range [0, 10). 首先创建一个指定大小的向量,并使用std::iota
将其填充为[0,10)范围内的值。
std::vector<int> v(10);
std::iota(v.begin(), v.end(), 0);
Then we use std::random_shuffle
to reorder the values. 然后,我们使用std::random_shuffle
重新排列值。
std::random_shuffle (v.begin(), v.end());
And pick the top three values: 并选择前三个值:
for (int i = 0; i < 3; ++i)
std::cout << v[i] << " ";
I would do something like this: 我会做这样的事情:
int a = rand() % 7 + 1;
int b = rand() % 8 + 1;
while( a == b )
b = rand() % 8 + 1;
int c = rand() % 9 + 1;
while(( a == c ) || ( b == c ))
c = rand() % 9 + 1;
int array[3] = { a , b , c };
Your check for "bad values" should be: 您检查的“不良价值”应为:
if ( ( a == b) || ( a == c ) || ( b == c ) ) 如果((a == b)||(a == c)||(b == c))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.