众所周知的java中的random()方法获得的是“伪随机数”,在C++里可以用设置seed的方法
众所周知的java中的random()方法获得的是“伪随机数”,在C++里可以用设置seed的方法来获得“真的随机数(我觉得也是伪随机数,不过伪的比较真)”,但是java...
众所周知的java中的random()方法获得的是“伪随机数”,在C++里可以用设置seed的方法来获得“真的随机数(我觉得也是伪随机数,不过伪的比较真)”,但是java里这个方法似乎不能设置参数,求大神指导java怎么过得随机数。
展开
2个回答
展开全部
你用的是Math.random()吗?
Math只是个方便使用的工具类,真正生成随机数的是java.util.Random这个类,Math里也是使用这个类,只不过是封装好方便调用而已。
public static double random()
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.
When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression new java.util.Random() This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else. This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator.
java.util.Random这个类的构造函数本身就提供了seed参数,详见JAVA API
public Random(long seed)
Creates a new random number generator using a single long seed. The seed is the initial value of the internal state of the pseudorandom number generator which is maintained by method next(int).
The invocation new Random(seed) is equivalent to:
Random rnd = new Random();
rnd.setSeed(seed);
Parameters:
seed - the initial seed
Math只是个方便使用的工具类,真正生成随机数的是java.util.Random这个类,Math里也是使用这个类,只不过是封装好方便调用而已。
public static double random()
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.
When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression new java.util.Random() This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else. This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator.
java.util.Random这个类的构造函数本身就提供了seed参数,详见JAVA API
public Random(long seed)
Creates a new random number generator using a single long seed. The seed is the initial value of the internal state of the pseudorandom number generator which is maintained by method next(int).
The invocation new Random(seed) is equivalent to:
Random rnd = new Random();
rnd.setSeed(seed);
Parameters:
seed - the initial seed
追问
虽然是复制粘贴的,但是还有点用……
迈杰
2024-11-30 广告
2024-11-30 广告
GWAS,即全基因组关联分析,是一种强大的遗传学研究方法。它通过对大规模群体的DNA变异进行系统性扫描,寻找与特定性状(如疾病易感性、药物反应等)相关联的遗传变异。在迈杰转化医学研究(苏州)有限公司,我们利用先进的GWAS技术,挖掘疾病相关...
点击进入详情页
本回答由迈杰提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询