C++ 将rand()结果写入C++;没有重复的数字

C++ 将rand()结果写入C++;没有重复的数字,c++,random,ofstream,C++,Random,Ofstream,在我的程序中的这一点上,每当生成一个随机数时,我都会将数组中该数的内容写入一个文本文件。守则: #include <iostream> #include <string> #include <fstream> #include <cstdlib> #include <time.h> #include "inFrench.h" using namespace std; class student { int m_student

在我的程序中的这一点上,每当生成一个随机数时,我都会将数组中该数的内容写入一个文本文件。守则:

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <time.h>
#include "inFrench.h"

using namespace std;

class student
{
    int m_studentNumber;
public:
    string nameFirst;
    string nameLast;
    string nameFull;
    int getStudentNumber() { return m_studentNumber; }
    void setStudentNumber(int studentNumber) { m_studentNumber = studentNumber; }
};

ostream& operator<<(ostream& os, const student& s)
{
    return os << s.nameFirst << ' ' << s.nameLast;
}

student typeName()
{
    student bar;
    cout << "Type in a student's first name: ";
    cin >> bar.nameFirst;
    cout << "Type in that student's last name: ";
    cin >> bar.nameLast;
    bar.nameFull = bar.nameFirst + " " + bar.nameLast;
    return bar;
}

void displayStudents(student listOfStudents[50], int studentHeadCount)
{
    for (int i = 0; i < studentHeadCount; i++)
    {
        cout << listOfStudents[i].nameFull << endl;
        cout << listOfStudents[i].getStudentNumber() << endl;
        cout << "\n";
    }
}

void generateGroups(int numberOfGroups, int maxStudents, int studentsPerGroup, int remainder, student theStudents[], int studentsBeenAssigned, ofstream studentGroups)
{
    int k;
    numberOfGroups = maxStudents / studentsPerGroup;
    cout << numberOfGroups << endl;
    srand(time(NULL));
    studentGroups.open("studentGroups.txt");
    while (studentsBeenAssigned < maxStudents && studentsBeenAssigned < maxStudents - remainder)
    {
        for (int j = 0; j < maxStudents - remainder; j++)
        {
            k = rand() % maxStudents;
            cout << theStudents[k].nameFull << endl;
            studentGroups << theStudents[k].nameFull << endl;
            studentsBeenAssigned++;
            if (studentsBeenAssigned == studentsPerGroup)
            {
                cout << "\n";
                studentGroups << theStudents[k].nameFull << endl;
                studentsBeenAssigned = 0;
            }
        }
    }
    if (remainder < studentsPerGroup && remainder > 0)
    {
        for (int l = 0; l < remainder; l++)
        {
            cout << theStudents[k].nameFull << endl;
        }   
    }
}

void languageChoices()
{
    cout << "Select your language from the following:\n";
    cout << "a) English\n";
    cout << "b) French\n";
    cout << "\n";
}

void options()
{

    cout << "Select what you want to do:\n";
    cout << "1) Exit application\n";
    cout << "2) Enter a Student\n";
    cout << "3) Display Students\n";
    cout << "4) Display Groups\n";
    cout << "5) Output groups as text file\n";
    cout << "6) Reset number of students in class\n";
    cout << "7) Reset number of students per group\n";
    cout << "\n";
}

int main()
{
    char selectedLanguage;
    languageChoices();
    cin >> selectedLanguage;
    switch (selectedLanguage)
    {
        case 'a':
        {
            student allStudents[50]; // Having 50 students alone is ridiculous!
            bool endProg = 0;
            int maxStudents;
            int studentsPerGroup;
            int optionSelect;
            int studentHeadCount = 0;
            int remainder = 0;
            int numberOfGroups = 0;
            int studentsBeenAssigned = 0;
            ofstream studentGroups;
            cout << "GroupPicker 1.0\n";
            cout << "Note: This version of the program is intended for purposes of education only, " 
            << "specifically for teacher use in a classroom.\n\n";
            cout << "How many students are in the class?\n" << "(Note: You cannot have more than 50 in this program)\n";
            cin >> maxStudents;
            if (maxStudents > 50)
            {
                cerr << "Too many students!\n" << "Exiting program...\n";
                system("PAUSE");
                exit(1);
            }
            if (maxStudents >= 35 && maxStudents <= 50)
            {
                cout << maxStudents << " students? You are a pro!\n";
            }
            cout << "How many students per group?\n";
            cin >> studentsPerGroup;
            if (studentsPerGroup >= maxStudents || studentsPerGroup <= 1)
            {
                cerr << "You're kidding, right?\n" << "Exiting program...\n";
                system("PAUSE");
                exit(1);
            }
            while (endProg == 0) {
                options();
                cin >> optionSelect;
                switch (optionSelect) {
                    case 1:
                        endProg = 1;
                        break;
                    case 2:
                    {
                        if (studentHeadCount == maxStudents)
                        {
                            cerr << "You can't enter more than " << maxStudents << " students\n";
                        }
                        else
                        {
                            allStudents[studentHeadCount] = typeName();
                            allStudents[studentHeadCount].setStudentNumber(studentHeadCount);
                            cout << "Student (" << allStudents[studentHeadCount].nameFull << ") entered.\n";
                            cout << "\n";
                            studentHeadCount++;
                        }
                        break;
                    }
                    case 3:
                        cout << "Current list of students:\n\n";
                        displayStudents(allStudents, studentHeadCount);
                        break;
                    case 4:
                    {
                        if (studentHeadCount < studentsPerGroup || studentHeadCount < maxStudents)
                        {
                            cerr << "Invalid group parameters.\n" << "Returning to main menu...\n\n";
                            break;
                        }
                        else
                        {
                            cout << "Here are the groups:\n\n";
                            studentGroups.open("studentGroups.txt");
                            generateGroups(numberOfGroups, maxStudents, studentsPerGroup, remainder, allStudents, studentsBeenAssigned, studentGroups);
                        }
                        break;
                    }
                    case 5:
                    {
                        cout << "Saving groups to file...\n";
                        studentGroups.close();
                        break;
                    }
                    case 6:
                    {
                        cout << "How many students are in the class?\n" << "(Note: You cannot have more than 50 in this program)\n";
                        cin >> maxStudents;
                        if (maxStudents > 50)
                        {
                            cerr << "Too many students!\n" << "Try again...\n";
                        }
                        if (maxStudents >= 35 && maxStudents <= 50)
                        {
                            cout << maxStudents << " students? You are a pro!\n";
                        }
                        break;
                    }
                    case 7:
                    {
                        cout << "How many students per group?\n";
                        cin >> studentsPerGroup;
                        if (studentsPerGroup >= maxStudents || studentsPerGroup <= 1)
                        {
                            cerr << "You're kidding, right?\n" << "Try again...\n";
                        }
                        break;
                    }
                }
            }
            break;
        }
        case 'b':
        {
            mainInFrench();
        }
    }
}
#包括
#包括
#包括
#包括
#包括
#包括“INFORENCH.h”
使用名称空间std;
班级学生
{
int m_学生编号;
公众:
字符串名称优先;
字符串nameLast;
字符串nameFull;
int getStudentNumber(){返回m_studentNumber;}
void setStudentNumber(int studentNumber){m_studentNumber=studentNumber;}
};

ostream&operator您正在将流按值传递给
generateGroups()
函数,这将导致复制构造函数调用。无法复制标准流


至于问题的第二部分,你不能期望PRNG总是返回唯一的数字,更不用说用模数限制范围了。您需要编写更多的代码,以检查您得到的号码之前是否已经生成,或者换言之,某些学生已经分配了号码。

std::ofstream
无法复制。看起来您需要一个参考:

void generateGroups(int numberOfGroups, int maxStudents, 
                    int studentsPerGroup, int remainder, 
                    student theStudents[], int studentsBeenAssigned, 
                    ofstream& studentGroups)
{//                         ^
在大多数实现中,rand()使用的seed在2^32个周期或更多周期之前不会重复,但是(seed>>xx)%n将至少每n个周期重复一次,如果n与rand()使用的数字不是相对素数,则重复频率会更高


您可以创建一个n字节的数组,并使用它们来跟踪是否存在重复。

这已经删除了IntelliSense错误,但我仍然有我提到的第一个。@DJHead在这些消息中与同一个错误有关。是的,但我的编译器IDE似乎不这么认为。@DJHead好吧,我对此无能为力。实际上,现在我的编译器已经修复了自己。我知道我需要编写一个条件语句,但我对PRNG不够熟悉,不知道如何编写代码来检查重复项。我该如何编写这样一个数组?假设您调用数组num,初始化num[]zero,每次从rand获得一个数字,例如j=rand(),然后设置k=num[j]和num[j] = 1. 如果k==0,则j是一个新的数字,否则j是一个重复的数字。请注意,RAND_MAX通常为32767,如果您想要更大的范围,请根据我的答案中blue link的一个线性同余生成器创建您自己的RAND-like函数。好主意。在代码中会是什么样子?(我问这个问题是因为我无法从逻辑上思考如何写出j是一个新数字,以及为什么num[j]=1。)num[]以全零开始。每次从rand()中获取一个数字时,您都会将num[rand()]中的相应值设置为1,以指示该数字已被使用,如果再次看到该值设置为1,则表明该数字已被使用。就像直方图,但它只是假的或真的,而不是发生次数。
void generateGroups(int numberOfGroups, int maxStudents, 
                    int studentsPerGroup, int remainder, 
                    student theStudents[], int studentsBeenAssigned, 
                    ofstream& studentGroups)
{//                         ^