Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无重复数的随机数生成器_C++ - Fatal编程技术网

C++ 无重复数的随机数生成器

C++ 无重复数的随机数生成器,c++,C++,因此,基本上我必须制作一个数组,允许用户随机生成他们选择的数字数量,并且没有一个数字是相同的。我的代码正常生成它们,但仍然得到重复的数字,我不知道为什么,因为我认为我阻止了它。我对阵列还是相当陌生,所以这可能看起来非常愚蠢,任何帮助都将不胜感激!我留下了很多旁注,试图对每个部分进行分解,在底部我将包括它运行时的样子 #include <iostream> #include <ctime> #include <windows.h> using namespace

因此,基本上我必须制作一个数组,允许用户随机生成他们选择的数字数量,并且没有一个数字是相同的。我的代码正常生成它们,但仍然得到重复的数字,我不知道为什么,因为我认为我阻止了它。我对阵列还是相当陌生,所以这可能看起来非常愚蠢,任何帮助都将不胜感激!我留下了很多旁注,试图对每个部分进行分解,在底部我将包括它运行时的样子

#include <iostream>
#include <ctime>
#include <windows.h>
using namespace std;

int main()
{
    int numberlist[20]; //the array
    int count=0,amount=0,value=0;
    bool found=false;

    srand(time(NULL)); //makes randomizer not the same

    cout << "How many numbers to generate?" << endl;
    cin>>amount; //gets user input

    for(int count=0; count<amount; count++){
    value = rand() % 40 + 1; //generates random number from 1-40 until amount is reached
    found=false;

    if(value==numberlist[count])found=true; //if value is in the array, change found from false to true

    if(value!=numberlist[count] && found==false){ //if number is unique and new
    numberlist[count]=value;                   //add number to array
    cout << value << endl; //show the value to the screen
    }
    else if (found==true) {count--;} //if value is in array erase 1 from count

    }
    return 0;
}

//What it looks like altogether
//How many numbers to generate?
// 9 (<the users input)
//37
//5
//30
//13
//7
//18
//1
//25
//25 (The 25 is the repeating number in this case)

#包括
#包括
#包括
使用名称空间std;
int main()
{
int numberlist[20];//数组
整数计数=0,金额=0,值=0;
bool-found=false;
srand(time(NULL));//使随机化器不同
cout amount;//获取用户输入

对于(int count=0;count,您的逻辑有错误

if(value==numberlist[count])found=true;//如果值在数组中,将found从false更改为true
这是唯一的检查,如果在位置等于计数上没有重复项,则必须迭代所有数组位置

完整代码如下:

#include <iostream>
#include <ctime>

using namespace std;

int main()
{
    int numberlist[20]; //the array
    int count=0,amount=0,value=0;
    bool found=false;

    srand(time(NULL)); //makes randomizer not the same

    cout << "How many numbers to generate?" << endl;
    cin>>amount; //gets user input

    for(int count=0; count<amount; count++){ 
    value = rand() % 40 + 1; //generates random number from 1-40 until amount is reached
    found=false;

    for(int i = 0 ; i < count; i++) // iterate over all position in array
    if(value==numberlist[i])found=true; //if value is in the array, change found from false to true

    if(found==false){ //if number is unique and new <--- I have also fix this condition
    numberlist[count]=value;                   //add number to array
    cout << value << endl; //show the value to the screen
    }
    else if (found==true) {count--;} //if value is in array erase 1 from count

    }
    return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
int numberlist[20];//数组
整数计数=0,金额=0,值=0;
bool-found=false;
srand(time(NULL));//使随机化器不同
cout amount;//获取用户输入

对于“int计数=0;如果是“代码>金额”>40代码/代码>复制,则可以保证:一个40元素的数组。用1到40填充。从数组中挑选出第一个<代码>金额>代码>项目。下面的例子展示了如何充分利用C++标准库来完成所有的工作。要生成的值,以任何适当的方式生成该数量的唯一值(例如,到数组或其他容器中)。然后随机洗牌该容器,并按顺序绘制值。随机数生成器通常会给出重复值-“生成随机值”和“避免重复”在某种程度上,这些要求是相互矛盾的。@namikaze123-如果你想“学习基础知识”,阅读标准算法的文档是有价值的-因为在实践中,一些网站(如en.cppreference.com)提供了算法的示例实现。这不会改变你的方法的基本事实(生成随机数,在有足够的数据之前丢弃重复数据)不能保证工作(例如,如果您在限定的时间内需要这些值,您可能需要在生成足够的值之前终止循环)。然而,生成一组唯一值并混洗(使用rng)完全避免此类问题。Ofc,我只修复了您的逻辑。可能您刚刚开始学习编程,您需要最简单的解决方案,没有集合或类似的东西(可能更有效)您可以提供两种代码变体,一种是简单的,另一种是优雅的。啊,我看到了,在底部我使用count——删除最新的重复,这样它就不会真正删除数字?那么我该如何删除数字本身,将numberlist[count]-1工作?感谢您的帮助!这里有一个很大的例子,您不必删除该数字。不将其放入数组就足够了:)。您的for循环每次都会递增计数,这要归功于计数--;您选择了大量整数(如果值已经在数组中)。我建议使用while循环而不是for循环,并且仅当find==false时才增加计数