English 中文(简体)
我如何从X/Y坐标处产生一种假象?
原标题:How can I produce a pseudorandom pattern from X/Y coordinates deterministically?

我写的是一张小片,偶尔在2D地图上点燃。 (“火park”只是一个更光明的col。) 我像park块一样,随意和统一地在(有限)平面上分配,但我希望,火焰在X和Y坐标的基础上是决定性的。 我尝试从坐标处创造种子,并从这一种子中创建 JavaRandom,但迄今为止,我的努力已形成了可识别的模式。 这一职能经常被称作(几百万倍),因此业绩至关重要。

首先,我尝试将我的<代码>hashCode()执行付诸东流,使用一个数字乘数避免碰撞。 由此,在地图上出现了明显可见的气体,其中一系列点共用同样的种子。

I then tried to create a seed by concatenating the coordinates like so:

long seed = ((long) x << 32) | (long) y;
Random rand = new Random(seed);

这似乎也产生了模式数据,尽管模式是显而易见的。 选定的坐标线上显示,根本不平均分配。

我避免使用MD5或其他加密洗衣算法,因为我害怕履约影响。

问题回答

下面是将环形混为一谈的非常有效的功能:

public static final long xorShift64(long a) {
    a ^= (a << 21);
    a ^= (a >>> 35);
    a ^= (a << 4);
    return a;
}

因此,如果你想要一个假肢,那么你可以做的是:

    long mix = xorShift64(x) + Long.rotateLeft(xorShift64(y),32) + 0xCAFEBABE;
    long result = xorShift64(mix);

此前,我成功地将这一方法用在图表中,结果很好! 随机数字的质量与java一样。 Rand,但速度要快得多。

http://en.wikipedia.org/wiki/Linear_congruential_generator” rel=“nofollow”>tlinear congruential generator rel=“nofollow”>.java.util. 鉴于这些声明,

private static final int SEED = 42;
private static final int N = 128;
private static final int MAX_X = 1024;
private static final int MAX_Y = 1024;
private final Random rnd = new Random(SEED);
private final List<SparklePoint> list = new ArrayList<SparklePoint>(N);

您可以初步编制<代码>的(答复)清单。 N 随机选取的圆条码中点(0,0, ×, ±_Y) 如下:

public void init(int seed) {
    for (int i = 0; i < N; i++) {
        int x = rnd.nextInt(MAX_X);
        int y = rnd.nextInt(MAX_Y);
        list.add(new SparklePoint(x, y));
    }
}

It may be convenient to give each point a Timer whose period is chosen from the same sequence:

private class SparklePoint implements ActionListener {

    private static final int MAX_DELAY = 1000;
    private final Point p;
    private final Timer t;
    private boolean bright;

    public SparklePoint(int x, int y) {
        p = new Point(x, y);
        t = new Timer(rnd.nextInt(MAX_DELAY), this);
        t.setRepeats(false);
        t.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        t.stop();
        if (bright) {
            // darken p
        } else {
            // brighten p
        }
        bright = !bright;
        t.setDelay(rnd.nextInt(MAX_DELAY));
        t.start();
    }
}

这是我所做的工作(产生预期的效果),但肯定是完美的。

MessageDigest md5;
try {
    md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
    return null;
}
md5.update(new byte[] {
    (byte)(x >>> 24),
    (byte)(x >>> 16),
    (byte)(x >>> 8),
    (byte)x,
    (byte)(z >>> 24),
    (byte)(z >>> 16),
    (byte)(z >>> 8),
    (byte)z
}, 0, 8);
byte[] digest = md5.digest();
long seed = digest[0] + (digest[1] << 8) + (digest[2] << 16) + (digest[3] << 24) + (digest[4] << 32) + (digest[5] << 40) + (digest[6] << 48) + (digest[7] << 56);
Random random = new Random(seed);

Aside from being particularly verbose, the use of the Random is probably excessive since I only pull call nextInt() twice. It s useful for generating values in a specific range, but I should be able to do that with modulo arithmetic anyway.

我同样认为,MD5是一种众所周知的算法,而加密安全对于这一应用并不重要。 不过,我肯定喜欢更快一点(而且不太大)。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签