- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想编写一个算法来模拟现场投票。
观众有 4 个不同的选项可以投票。我可视化每个选项 a、b、c、d 的百分比。
所以我想出了一个算法,可以给我一个由 4 个随机数组成的数组,总和为 100。
dataset = [];
a = randombetween(0, max);
b = randombetween(0, max - a);
c = randombetween(0, max - a - b);
d = max - a - b - c;
dataset.push(a, b, c, d);
shuffle(dataset);
此代码每 2 秒执行一次以更新轮询结果。
为了使我的算法更加逼真,我希望更改更加渐进。我想创建一个初始随机结果,然后逐渐增加/减少投票结果。假设投票结果每次更新增加/减少不应超过 5%。
这里有一个可以玩的 fiddle :Live Poll
最佳答案
为什么不模拟提交投票的恒定比率?这样,当你计算百分比时,当有更多人投票时,变化会更小。这似乎比更改百分比更有效。我把它放在一起作为演示 (https://jsfiddle.net/ovkfw577/1/)
var votes = [0,0,0,0];
var dataset = [0,0,0,0];
var dataSize = 4;
var voteRate = 10;
// Algorithm to simulate the further voting process
var updateData = function() {
var total = 0;
dataset = [0,0,0,0];
// add random amount of votes
for(i=0; i < dataSize; i++) {
var randomVotes = randombetween(0,voteRate);
votes[i] += randomVotes;
total += votes[i];
}
// calculate percentages
for(i=0; i < dataSize; i++) {
dataset[i] = votes[i] / total * 100;
}
// so poll bars don't completely stall later on.
// not entirely necessary, but helps polling
// appear more active.
voteRate *= 1.2;
drawVis();
$("svg").fadeIn("slow");
};
关于模拟现场投票的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35366599/
我刚接触一个项目,开发人员仍然通过邮件发送文件来共享代码。我们正在使用 eclipse 和 cvs。 场外的开发人员将代码发送到现场进行审核,其他开发人员从那里的邮件中一一获取文件并在 Eclipse
我完成了 session 站点设计,现在应该创建数据库。但我不确定该使用哪种方法。我正在使用 Apache、PHP 和 MySQL。我有用户,每个用户都有输入和输出邮箱,每个用户都有一个好友列表。这是
我们的开发团队刚刚将一个应用程序从本地服务器迁移到实时站点。该应用程序利用可写入的远程数据库,并利用 MSQLi 和 PDO 方法获取数据并将数据推送到数据库。 更新 connect .inc 文件以
大家好,我想使用 java 连接到我网站上的数据库。我想知道我应该在 URL 中输入什么: cnt=DriverManager.getConnection(DB_URL, DB_UID,DB_PWD)
我不确定“硬失败”是否是正确的词,但这是我面临的问题。我花了相当长的时间来重现这个尽可能最小的例子,所以它是这样的: class BaseParameterizedType fun > getSpec
我希望有人能够帮助解决我遇到的问题。下面的代码管理我正在开发的网站上的音频播放器。这个想法是,用户可以单击静音按钮来切换网站上的声音,如果用户单击特定链接离开网站,则声音应该被静音(如果尚未静音)。
我在站点中使用 libcurl 进行身份验证。我用它制作了很多功能,例如: char *CheckLoginPass(char *login, char *pass) { //use curl_e
我正在创建一个社交媒体风格的网站,并使用 Meteor 和 MongoDB 创建帖子,其中包含 {{title}} 和 {{content}}一个 Accordion 菜单,标题在菜单的标题上,内容在
我昨天为一个网站写了一个小的桌面 package 器(它实际上只是加载一个网站),作为对 Electron 的一些练习,但我注意到以下事情: 通常应该(并且确实)在每个浏览器上工作的 Javascri
我的应用程序中有 WebView 。我可以在 IDE 中看到警告。“使用 setJavaScriptEnabled 会引入 XSS 漏洞”代码 webView.getSettings().setJav
我正在做一个多步骤表单,所有内容都在最后保存。在我的模型中,我有一个 m2m 复选框字段,并且我使用 django Sessions 来获取表单数据以在最后一步中显示它。 问题是当我提交最终表单时,m
我们已经实现了 WURFL 现场 Java 应用程序(使用 maven/servlet 实现的 berryapp),我们将其用于所有 Java 应用程序,只需在所有其他应用程序中调用该应用程序即可。由
我是一名优秀的程序员,十分优秀!