C++ 人口数据的生成方法
我必须生成100行城市人口,然后为每个城市生成相同数量的失业人口行。数据应该接近真实情况。我的问题是如何正确地做到这一点?我有一个想法,我想与你分享,以获得你的意见。例如: 我将从中央统计局获取200行特定城市的真实人口数据,然后从这200行中随机选择100行。在此之后,我还将随机生成有关失业的数据,但基于早期的人口数据,考虑到失业人数可能不会超过人口数量 此时,我随机生成了1000到30000(人口)的数据,如下所示:C++ 人口数据的生成方法,c++,algorithm,random,C++,Algorithm,Random,我必须生成100行城市人口,然后为每个城市生成相同数量的失业人口行。数据应该接近真实情况。我的问题是如何正确地做到这一点?我有一个想法,我想与你分享,以获得你的意见。例如: 我将从中央统计局获取200行特定城市的真实人口数据,然后从这200行中随机选择100行。在此之后,我还将随机生成有关失业的数据,但基于早期的人口数据,考虑到失业人数可能不会超过人口数量 此时,我随机生成了1000到30000(人口)的数据,如下所示: int random_population_result = (rand(
int random_population_result = (rand() % 29000) + 1000;
失业人数从100人到2000人不等
int random_unemployed_result = (rand() % 1900) + 100;
但我的教授对我说,以这种方式生成此类数据不是一个好主意,所以他让我考虑一下。我在上面向你介绍了我的新想法,我很好奇你的意见
整个循环:
//number of rows in column
const int colSize = 100;
int col_X[colSize]; //stores X values [population]
int col_Y[colSize]; //stores Y values [unemployed people]
//display table header
cout << "id " << "\t" << "X" << "\t" << "Y" << endl;
for (int i = 0; i < colSize; i++){
//return value between 1000 and 30 000 of population
int random_population_result = (rand() % 30000) + 1000;
//return value between 100 and 2000 of unemployed people
int random_unemployed_result = (rand() % 1900) + 100;
//put values to arrays
col_X[i] = random_population_result;
col_Y[i] = random_unemployed_result;
}
//列中的行数
常数int colSize=100;
int col_X[colSize]//存储X值[总体]
国际货币基金组织[货币基金组织]//价值观[失业者]
//显示表格标题
看看极端情况。人口最少的是1000人,失业人数最多的是2000人。显然,这是一个问题,因为1000人中不可能有2000人失业
在另一个极端,在一个30000人的城市里,可能有100人失业,失业率为0.3%。这比你在现实生活中发现的要低得多
因此,失业率应该与城市人口成比例。在现实生活中,失业率通常以人口的百分比表示,城市之间略有差异。例如,平均失业率可能为10%,A市失业率可能为9%,B市失业率可能为12%
因此,选择你所在城市的人口,然后选择一个失业率,然后将这两个结果相乘,得到失业人数。正如用户3386109所说,你可能需要一个现实的数据集
首先,你想根据人口的结果创造你的失业率,所以
int random_population_result = (rand() % 30000) + 1000;
int random_unemployed_result = (rand() % (random_population_result-100)) + 100;
但是,如果你想考虑到失业率只能在1%到20%之间,你可以添加以下内容:
int minPercent = 1;
int maxPercent = 20;
int random_population_result = (rand() % 30000) + 1000;
int random_unemployed_result = (rand() % ((maxPercent-minPercent)*random_population_result/100)) + minPercent*random_population_result/100;
因此,更新后的结果将是:
int col_X[colSize]; //stores X values [population]
int col_Y[colSize]; //stores Y values [unemployed people]
//display table header
//cout << "id " << "\t" << "X" << "\t" << "Y" << endl;
for (int i = 0; i < colSize; i++){
//return value between 1000 and 30 000 of population
//(ile_liczb_w_przedziale ) + startowa_liczba;
int minPercent = 1;
int maxPercent = 20;
int random_population_result = (rand() % 30000) + 1000;
int random_unemployed_result = (rand() % ((maxPercent-minPercent)*random_population_result/100)) + minPercent*random_population_result/100;
//put values to arrays
col_X[i] = random_population_result;
col_Y[i] = random_unemployed_result;
}
int col_X[colSize]//存储X值[总体]
国际货币基金组织[货币基金组织]//价值观[失业者]
//显示表格标题
//你以前的方法是什么?那边是什么?你的教授除了说不好之外还说了什么吗?只是一个评论:如果你想要严重的随机性,不要使用rand()
,看看为什么,而是看看你的真实人口数据是否有城市失业率的值?@Jean Emmanuel我刚刚在上面的主要帖子中添加了整个循环,这就是“那样”。同样是的——这就是他此刻所说的一切。@pjs否,但当我有了真实的人口数据时,它将更容易生成。我是说我想是的。