Java在不同jvm或不同机器上的Math.random()有多随机 [英] how random is Math.random() in java across different jvms or different machines

查看:69
本文介绍了Java在不同jvm或不同机器上的Math.random()有多随机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在许多不同的物理服务器上都有一个大型的分布式程序,每个程序都产生许多线程,每个线程在其操作中都使用Math.random()从许多公共资源池中提取一个片段.

I have a large distributed program across many different physical servers, each program spawns many threads, each thread use Math.random() in its operations to draw a piece from many common resource pools.

目标是在所有操作中平均利用池.有时,通过查看资源池上的快照来查看它在那一刻正在获得的片段,看起来似乎不是那么随机(实际上可能是,但是很难确定并找出来).

The goal is to utilize the pools evenly across all operations. Sometimes, it doesn't appear so random by looking at a snapshot on a resource pool to see which pieces it's getting at that instant (it might actually be, but it's hard to measure and find out for sure).

有什么东西比Math.random()好,并且表现也一样好(至少没有差很多)?

Is there something that's better than Math.random() and performs just as good (not much worse at least)?

推荐答案

Math.random()基于java.util.Random,而java.util.Random基于

Math.random() is based on java.util.Random, which is based on a linear congruential generator. That means its randomness is not perfect, but good enough for most tasks, and it sounds like it should be sufficient for your task.

但是,听起来您正在使用double返回值Math.random()在固定数量的选项之间进行选择,这可能会进一步降低随机性的质量.最好使用 java.util.Random.nextInt() -只要确保重复使用同一Random对象即可.

However, it sounds like you're using the double return value of Math.random() to choose between a fixed number of choices, which may further degrade the quality of the randomness. It would be better to use java.util.Random.nextInt() - just be sure to reuse the same Random object.

有时候,通过查看资源池上的快照来查看它在瞬间获得了哪些片段,它看起来并不是那么随机

Sometimes, it doesn't appear so random by looking at a snapshot on a resource pool to see which pieces it's getting at that instant

我们的大脑非常擅长以完全随机的方式发现模式,因此几乎没有任何意义.

Our brains are really good at spotting patterns in perfect randomness, so that means almost nothing.

这篇关于Java在不同jvm或不同机器上的Math.random()有多随机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆