模拟垃圾分布

 

// TestRandomGC.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <iostream>
#include <ctime>
#include <stdio.h>
#include <string>

using namespace std;

/* 已知跑了20个小时后,垃圾量的分布情况,20%垃圾量的档位有22%,22%-50%垃圾量的比较平均了都是5%左右,52的%垃圾量有1%
    我们要实现一个可以配置的表(垃圾量分布图),配置不同垃圾量的占比是多少。我们就按照这个表,去随机生成这种垃圾量占比。*/
unsigned int randArrays[100] = {20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
                                                       20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
                                                       20, 20, 22, 22, 22, 22, 24, 24, 24, 24,
                                                       26, 26, 26, 26, 28, 28, 28, 28, 28, 30,
                                                       30, 30, 30, 30, 32, 32, 32, 32, 32, 34,
                                                       34, 34, 34, 34, 36, 36, 36, 36, 36, 38,
                                                       38, 38, 38, 38, 40, 40, 40, 40, 40, 42,
                                                       42, 42, 42, 42, 42, 44, 44, 44, 44, 44,
                                                       44, 46, 46, 46, 46, 46, 46, 48, 48, 48,
                                                       48, 48, 48, 48, 48, 50, 50, 50, 50, 52};

void initVector(vector<unsigned int> &vec, unsigned int size) 
{
    srand(unsigned(time(NULL)));
    for(unsigned int i =0; i < size; i++)
    {
        int randNum = rand()%100;/* 0-99的随机数作为垃圾量分布图的下标*/
        vec.push_back(randArrays[randNum]);
    }
}

void printVector(vector<unsigned int> vec)
{
    vector<unsigned int>::iterator it = vec.begin();
    for(; it != vec.end();++it)
    {
        printf("%d,", *it);
    }
    cout<<endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
    vector<unsigned int> vect;
    initVector(vect, 100);
    cout<<"before sort"<<endl;
    printVector(vect);
    sort(vect.begin(), vect.end());
    cout<<"after sort"<<endl;
    printVector(vect);
    return 0;
}

 

posted on 2017-04-10 21:16  cleverlzc  阅读(165)  评论(0编辑  收藏  举报