如何防止重复随机整数

时间:2016-12-23 00:41:10

标签: java arrays

"angular": "angularjs#^1.5.8",
"angular-route": "1.5.8",
"angular-sanitize": "1.5.5",
"swagger-js": "2.1.14",
"alertify.js": "alertifyjs#^1.0.10",
"angular-cookies": "1.5.8",
"angular-paging": "2.2.1",
"ng-table": "0.8.3",
"lodash": "4.16.4",
"ng-file-upload": "12.2.12",
"ngInfiniteScroll": "1.3.0",
"select2": "3.4.5",
"textAngular": "1.5.1",
"angularjs-slider": "5.5.1",
"angular-ui-calendar": "1.0.2",
"font-awesome": "4.6.3",
"angular-bootstrap-datetimepicker": "1.0.1",
"angular-validation": "1.4.2",
"raven-js": "3.7.0",
"keen-js": "3.4.1",
"angular-keenio": "0.2.0",
"angular-translate": "2.12.1",
"moment-timezone": "0.5.5",
"moment": "2.15.1",
"jquery": "3.1.1",
"fullcalendar": "3.0.1",
"angularjs-datetime-picker": "0.1.20",
"angular-moment": "^1.0.0",
"angular-ui-select": "^0.19.4",
"angular-ui-select3": "ui-select#*",
"pubnub": "^4.3.2",
"pubnub-angular": "^4.0.2",
"angular-moment-picker": "moment-picker#^0.8.4"

这会在0到9之间生成5个整数。如何生成它以便在生成它时不会有重复的整数?

2 个答案:

答案 0 :(得分:1)

  • 创建一个包含0到9
  • 的10个元素的数组
  • 随机播放阵列
  • 采取前五个要素

在伪代码中

array = [0, 1, ..., 9]
array.shuffle.take(5)

答案 1 :(得分:0)

您必须记录已选择的号码。

您可以这样做的一种方法是将值存储在布尔数组中,其中所有值都初始化为<div id="results"> </div>。生成随机值时,该索引处的元素将设置为false。然后,对于每个生成的数字,您只需检查该索引处的元素是true还是true

例如,

false