2017-10-14 57 views
3

我是新来编程和堆栈溢出。所以我一直在建立一个简单的数据库为我自己的享受和实践我的知识。它注册用户的用户名,密码并分配用户ID。对于文本文件,您也可以查看用户的信息。随机整数写入文本文件而不重复

我的主要问题是我想让程序给用户分配一个从1-1000(例如1000是最大员工)随机的ID。

当用户注册新的用户名和密码时,将执行此代码块。该程序输出用户名,密码和用户ID。我能够让程序为每个用户输出一个随机数,但是我无法让它产生没有重复的数字。

该程序应该输出一条消息,如果没有更多的用户ID可用,那么注册过程将不会完成。

我创建了一个函数,打印一束线条来清除屏幕,但一切都在底部。如果我可以使用不同的功能,我会很高兴知道!

if (passwordCheck == password) { 
      const int minEmp = 1, maxEmp = 10;   
      srand(time(0)); 
      userID = (rand() % (maxEmp - minEmp + 1)) + minEmp; 

      ofstream outfile (username + ".txt"); //Creates txt file with user name 
      outfile << "User ID: " << userID << endl << //Outputs info to txt file 
       "Account Username: "<< username << endl 
       << "Account Password: " << password; 
      outfile.close(); 
      clearScreen(); //My function to add lines to clear the screen 

      cout << endl << "Your account has been created." << endl 
       << endl << "Returning to main menu."; 

      clearScreen(); 
      mainMenu(); //Function to return back to menu 
+0

我没有得到实际的问题,你想为每个用户生成一个唯一的随机数? –

+3

“_我的主要问题是我想让程序给用户分配一个随机的ID,比如说1-1000_”并且..这里的问题在哪里?如果您想从该范围生成唯一的ID - 最简单的解决方案是记住/读取所有使用的ID,并生成ID,直到您获得一个ID,该ID不在该“使用的ID”列表中。你尝试过吗? –

+5

你应该只调用'srand'一次。为什么你不能从'1'开始分配ID并简单地递增? – rustyx

回答

2

你的基本问题是随机数的产生并不能保证唯一性。它当然不能保证程序运行之间的唯一性(例如,如果要在多次运行程序期间修改数据库并保持一致性)。因此,您需要一种生成一组唯一ID的方法,删除以前使用过的任何(如在数据库中表示的),然后随机洗牌。

一种方法可能是;

  1. 创建一个1000(或任何你需要的数字)唯一ID(例如在一个向量中)的列表。这可以通过一个简单的循环来完成,或者使用带有适当的发生器的std::generate(),每次调用它时都会返回不同的值。生成唯一ID的方法需要一致(无随机性)才能使第二步工作。
  2. 读取数据库并从列表中删除数据库中的每个ID。
  3. 如果剩余列表中有零个元素,则不允许添加更多 用户。
  4. 随机清单。在C++ 11或更高版本中,使用std::shuffle()。您需要阅读随机数生成 。在C++ 14之前,您可能会使用std::random_shuffle(),但请记住它使用rand() 来生成随机值,并且其质量不确定 - 这就是为什么它在C++ 14中被弃用的原因(标记为将来从标准中删除 )。
  5. 每当您需要新的唯一值(例如创建用户时) 从列表中获取最后一个值时,从 列表中删除该值,创建用户并将其保存到数据库。
+0

这正是我正在寻找的矢量。我还没有听说过他们,但自从答案以来,我一直在阅读它们,并会尽快将它们应用到我的程序中。非常感谢你! –

1

有一个棘手的事情得到的是:让你想作为一个排列随机生成的10个值,并且希望将它们存储在一个文件中说。

所以,你可以计算出如何生成随机指标则使用索引值的阵列上存储数据:

我也面临一些同样的问题,有一天,当我开发卡的游戏的图像,每个失败的隐藏图像随机跳转到某些位置。

#include <iostream> 
#include <fstream> 
#include <ctime> 


int main(){ 

    srand(time(NULL)); 

    std::ofstream out("data.txt"); 

    // arrays of data whose values will be stored randomly in a text file. 
    int array[10] = {7, 57, 23, 21, 1, 
        0, 18, 19, 3, 777}; 

    // array of indexes and initializing it 
    int indexes[10]; 
    for(int i(0); i < 10; i++) 
     indexes[i] = rand() % 10; 

    // indexes are filled with random values from 0 to 9 but it surely contains duplicates eg: 

    // 3, 0, 7, 5, 2, 8, 0, 1, 0, 0 


    // Now I use my algorithm to discard duplicates and repeating until I get an array of unique indexes. 

    for(int i = 0; i < 10; i++){ 
     for(int j(0); j < 10; j++){ 
      if(indexes[i] == indexes[j] && i != j){ 
       indexes[j] = rand() % 10; 
       i = 0; 
      }   
     } 
    } 

    // check out that the indexes are unique and no duplicates there: 

    std::cout << "\n\nThe random indexes: " << std::endl; 

    for(int i = 0; i < 10; i++) 
     std::cout << indexes[i] << ", "; 

    // writing the random values of array using the random indexes array to the file: 

    for(int i = 0; i < 10; i++) 
     out << array[indexes[i]] << ", "; 

    // printing the random values of the array 
    std::cout << "\n\nThe random values: " << std::endl; 

    for(int i = 0; i < 10; i++) 
     std::cout << array[indexes[i]] << ", "; 

    out.close(); 

    std::cout << std::endl << std::endl << std::endl; 
    return 0; 
} 

输出:尝试多次运行该程序并查看结果。

// 1 
The random indexes: 
3, 9, 5, 1, 7, 6, 0, 4, 8, 2, 

The random values: 
21, 777, 0, 57, 19, 18, 7, 1, 3, 23, 

// 2 
The random indexes: 
5, 8, 0, 1, 3, 9, 4, 2, 6, 7, 

The random values: 
0, 3, 7, 57, 21, 777, 1, 23, 18, 19, 


// 3 
The random indexes: 
6, 7, 3, 1, 5, 2, 4, 9, 0, 8, 

The random values: 
18, 19, 21, 57, 0, 23, 1, 777, 7, 3, 
  • 我只提供了一个小例子所以,如果你想在1000个唯一值你的情况只是看0-1000作为唯一值的数组,所以你可以使用上面的算法:

    1// initializing 
    int randValues[1000]; 
    for(int i(0); i < 1000; i++) 
        randValues[i] = (rand() % 1000) + 1; // because you want to discard 0 
    

    现在得到的唯一值:

    for(int i(0); i < 1000; i++){ 
        for(int j(0); j < 1000; j++){ 
         if(randValues[i] == randValues[j] && i != j){ 
          randValues[j] = (rand() % 1000) + 1; 
          i = 0; // reset 
         } 
        } 
    }