英文:
How do I generate (x,y) coordinates with this multiplicative LCG?
问题
我在Java中开发了以下代码:
/**
* 使用给定参数的线性生成器方法。
* @param multiplier 种子乘数。
* @param seed 程序的初始种子;指示生成器在周期的哪个点开始。
* @param addition 添加到种子的附加值。
* @param modulus 使用的模数。
* @param num 要生成的数字数量。
* @return 包含生成值的向量。
*/
public static BigDecimal[] generadorLineal(BigDecimal multiplier, BigDecimal seed, BigDecimal addition, BigDecimal modulus, int num)
{
BigDecimal numeros[] = new BigDecimal[num];
numeros[0] = multiplier.multiply(seed);
numeros[0] = numeros[0].add(addition);
numeros[0] = numeros[0].remainder(modulus);
for(int i = 1; i < num; i++){
numeros[i] = multiplier.multiply(numeros[i-1]);
numeros[i] = numeros[i].add(addition);
numeros[i] = numeros[i].remainder(modulus);
}
/*for(int i = 0; i < num; i++){ //归一化(0,1)
numeros[i] = numeros[i].divide(modulus.subtract(BigDecimal.valueOf(1)), 2, RoundingMode.HALF_UP);
}*/
return numeros;
}
并且有以下示例:
我生成了50个随机点,实际上有八个:
- p1(x1,y1) = (5,5)
- p2(x2,y2) = (25,25)
- p3(x3,y3) = (29,29)
- p4(x4,y4) = (17,17)
- p5(x5,y5) = (21,21)
- p6(x6,y6) = (9,9)
- p7(x7,y7) = (13,13)
- p8(x8,y8) = (1,1)
接下来的42个点是相同的周期。这是我得到的输出:
但是,我的老师告诉我结果是错误的;因为应该只有五个点,并且在我的画布的坐标x、y中分布更广;或者换句话说,我的点数是错误的,我的坐标“y”不正确。我确实不明白为什么只应该有五个不同的点,或者如何生成我的“y”坐标。
如果该LCG生成的周期为八,这是否意味着程序为我的画布生成了八个不同的“x”坐标?我非常困惑,我不是在寻求代码;而是要解释一下为什么我的输出不正确。
英文:
I developed the following code in Java:
/**
* Método que sirve como generador lineal con unos parámetros dados.
* @param multiplier multiplicador de la seed dada.
* @param seed semilla inicial del programa; indicará en que punto del periodo comienza el generador.
* @param addition adición adicional a la semilla.
* @param modulus módulo usado.
* @param num cantidad de numeros a generar.
* @return un vector con los valores generados.
*/
public static BigDecimal[] generadorLineal(BigDecimal multiplier, BigDecimal seed, BigDecimal addition, BigDecimal modulus, int num)
{
BigDecimal numeros[] = new BigDecimal[num];
numeros[0] = multiplier.multiply(seed);
numeros[0] = numeros[0].add(addition);
numeros[0] = numeros[0].remainder(modulus);
for(int i = 1; i < num; i++){
numeros[i] = multiplier.multiply(numeros[i-1]);
numeros[i] = numeros[i].add(addition);
numeros[i] = numeros[i].remainder(modulus);
}
/*for(int i = 0; i < num; i++){ //normalization (0,1)
numeros[i] = numeros[i].divide(modulus.subtract(BigDecimal.valueOf(1)), 2, RoundingMode.HALF_UP);
}*/
return numeros;
}
And having the next example:
I generate 50 randomly points; being actually eight:
- p1(x1,y1) = (5,5)
- p2(x2,y2) = (25,25)
- p3(x3,y3) = (29,29)
- p4(x4,y4) = (17,17)
- p5(x5,y5) = (21,21)
- p6(x6,y6) = (9,9)
- p7(x7,y7) = (13,13)
- p8(x8,y8) = (1,1)
The next 42 points are the same period. This is the output that I get:
But, my teacher is telling me that the result is wrong; because it should only be five points and more distributed in the coordinates x,y of my canvas; or put it in another way, my number of points is wrong and my coordinate 'y' is not right. I certainly don't understand why it should be only five different points or how should I generate my 'y' coordinate.
If the period generated by that LCG is eight, doesn't it mean that the program is generating eight different 'x' coordinates for my canvas? I'm really confused and I'm not asking for code; but for some explanation of why my output isn't right.
专注分享java语言的经验与见解,让所有开发者获益!
评论