我正在尝试以这种方式在-0.5和0.5之间创建一个随机数:
public static double RandomNumberGenerator(long seed)
{
Random r = new Random(seed);
return r.nextDouble() >= 0.5? 0.5 : -0.5;
//PRINTING AND USAGE, SHOULD GO ABOVE
//System.out.println("Object after seed 12222222: " + RandomNumberGenerator(12222222) );
}
然后我按照这样执行:
for (int j = 0; j < weights_one.length ; j++)
{
weights_one[j] = RandomNumberGenerator( System.currentTimeMillis() );
}
但它确实功能失调,因为我总是以相同的权重结束,即
weights_one[0]: -0.5
weights_one[1]: -0.5
weights_one[2]: -0.5
weights_one[3]: -0.5
weights_one[4]: -0.5
这不好,它们应该是高斯分布式的,我怎样才能做到这一点?
答案 0 :(得分:1)
随机类创建伪随机数。我在你的代码中发现了两个错误:
您的代码可以替换为:
Random r = new Random(System.currentTimeMillis());
for (int j = 0; j < weights_one.length ; j++)
{
weights_one[j] = r.nextDouble() -0.5;
}