什么代码可以防止随机列表中的重复项MIT App Inventor

时间:2016-03-29 10:31:46

标签: list random app-inventor

我试图从列表中选择一个随机项目,但经过几次按钮点击后,我得到重复的项目,有时相同的项目会连续出现两到三次。我可以使用哪些代码来停止重复列表中的项目,除非已经完成了完整列表?

这是我的代码: Random item in list

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以使用随机shuffle算法来初始使用一些随机密钥对列表进行排序,然后迭代排序列表 - 所有项目都将是伪随机的。维基文章中有更多详细信息:https://en.wikipedia.org/wiki/Fisher - Yates_shuffle

答案 1 :(得分:0)

How to pick a random item from a list without picking duplicates?

在此示例中,我使用<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp"> <div ng-controller="mainCtrl"> <div class="row"> <input ng-model="firstName" placeholder="Enter a first name"> <input ng-model="lastName" placeholder="Enter a last name"> <button ng-click="changeBothNames(firstName, lastName)">Update Name</button> </div> <div class="row"> <div id="output" ng-if="fullName = ' ' "> <div id="greeting">{{firstName}} {{lastName}}</div> </div> <div id="output" ng-if="fullName != ' ' "> <div id="greeting">{{message}}</div>{{fullName}} </div> </div> </div>块而不是random integer块。选中的项目将在显示后从列表中删除。

enter image description here