国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++生成不重復的隨機整數

2020-01-26 13:24:25
字體:
來源:轉載
供稿:網友

C++生成不重復的隨機數,供大家參考,具體內容如下

給定正整數的范圍[n,m],生成k個不重復的隨機數字。

IDE是vs013。

#include "stdafx.h"#include <iostream> #include <vector>#include <stdlib.h> #include <time.h>#include<list>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ srand((unsigned)time(NULL)); list<int>::iterator it;//迭代器 list<int> l;//定義鏈表,保存生成的隨機數 int begin, end;//數字范圍 int sum;//隨機數個數 cout << "輸入數字范圍([n,m]):"; cin >>begin>>end; cout << "輸入隨機數個數:"; cin >> sum; if ( (end<0)||(begin<0)||(begin >end)|| (sum>end))//起始范圍必須大于0,且隨機數個數小于等于最大數字范圍 { cout << "范圍錯誤"; cout << endl; system("pause"); return 0; } else { while (l.size() < sum) { l.push_back(rand() % (end - begin + 1) + begin); l.sort();//排序 l.unique();//去除相鄰的重復隨機數中的第一個 } cout << "結果:"; } for (it = l.begin(); it != l.end(); it++) { cout << *it << ' '; }  cout << endl; system("pause"); return 0;}

運行結果:

這個程序可以用于班級內部按照學號進行隨機抽簽。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彝良县| 华宁县| 寻乌县| 泗水县| 平安县| 麻城市| 长阳| 琼中| 常熟市| 侯马市| 龙江县| 扎兰屯市| 峨眉山市| 胶州市| 奈曼旗| 黄梅县| 鄂尔多斯市| 栖霞市| 华蓥市| 古田县| 霍山县| 香格里拉县| 日喀则市| 济宁市| 施秉县| 东乡| 翼城县| 泾源县| 杨浦区| 定州市| 平利县| 泸水县| 永胜县| 洪泽县| 九龙县| 沈阳市| 建平县| 隆林| 汉源县| 南昌县| 河曲县|