C/C++ 实现随机数

   #include <iostream>
    #include <map>
    #include <stdlib.h> 
    #include <time.h>
    
    unsigned int getRandomNumber(int nModule)
    {
    	return rand() % nModule;
    }
    
    unsigned int getRandomNumberRand(int min, int max)
    {
    	return (rand() % (max - min + 1)) + min;
    }
    
    
    int main()                            //主函数
    {
    	map<int, int> ma;
    
    	int A[10], i;                         //整型数组和变量声明
    	int count = 0;
    	srand((int)time(NULL));              //设置系统时间为随机种子 此方法在主循环执行一次即可
    	for (i = 0; i < 10000; i++)              
    	{
    		int co = getRandomNumber(10);
    
    		map<int, int>::iterator it = ma.find(co);
    
    		if (it != ma.end())
    		{
    			it->second += 1;
    		}
    		else
    		{
    			ma.insert(make_pair(co, 1));
    		}
    
    	}
    
    	for (map<int, int>::iterator it = ma.begin(); it != ma.end(); it++)
    	{
    		cout << "随机数为:" << it->first << "  次数是:" << it->second << endl;
    	}
    
    	system("pause");
    	return 0;
    }
发布了43 篇原创文章 · 获赞 1 · 访问量 2321

猜你喜欢

转载自blog.csdn.net/lpl312905509/article/details/101203602