我在groovy中实施加权抽奖.它允许一些参与者比其他参与者有更好的获胜机会(基本上与NBA选秀一样).它的工作原理是将每个参与者投入阵列N次,其中N是您必须获胜的机会数.然后它从该数组中选择一个随机索引.
就像一个优秀的小编码器,我写了一个测试.它从该组中挑选100名获胜者,并输出每个参与者被选中的次数.期望它将大致与他们应该被挑选的次数(基于他们的机会数量)一致.结果是......关闭.
我将问题缩小到一行,如果分成两个单独的语句,则可以完美地运行.该例程的精简版本如下."坏"版本处于活动状态,"好版本"被注释掉
def randomInRange(int min, int max) {
Random rand = new Random()
rand.nextInt((max - min) + 1) + min
}
def bob = [name:'bob', timesPicked:0]
def joe = [name:'joe', timesPicked:0]
def don = [name:'don', timesPicked:0]
def chanceWheel = []
//don should get picked a lot more
2.times{chanceWheel << bob}
2.times{chanceWheel << joe}
6.times{chanceWheel << don}
//pick somebody at random from the chance wheel
100.times{
//this will produce timesPicked counts that do NOT sum to 100 and usually under-represents …Run Code Online (Sandbox Code Playgroud) 我需要扩展spring安全性以散列http响应内容并将结果放在标头内.我的方法是创建一个servlet过滤器,读取响应并放置适当的标头.过滤器通过单独的插件注册spring security.实施主要来自这里.
当最终应用程序在控制器中使用"render"将JSON输出到客户端时,整个设置工作正常.但是,如果通过"响应"格式化相同的数据,则将404返回给客户端.我无法解释这种差异.
作为参考,一切都是grails版本2.3.11和spring安全核心版本2.0-RC4
通过我的插件的doWithSpring注册过滤器
responseHasher(ResponseHasher)
SpringSecurityUtils.registerFilter(
'responseHasher', SecurityFilterPosition.LAST.order - 1)
Run Code Online (Sandbox Code Playgroud)
我的过滤器实现
public class ResponseHasher implements Filter{
@Override
public void init(FilterConfig config) throws ServletException {
}
@Override
public void doFilter(ServletRequest request,
ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletResponseCopier wrapper = new HttpServletResponseCopier((HttpServletResponse)response);
chain.doFilter(request, wrapper);
wrapper.flushBuffer();
/*
Take the response, hash it, and set it in a header. for brevity sake just prove we can read it for now
and set a static header
*/
byte[] copy …Run Code Online (Sandbox Code Playgroud)