1

我想不出一个好的方法来做到这一点,如果可能的话,我会很感激一些帮助!

恐怕我还没有任何代码可以发布,因为我还没有那么远。

我需要从0-999范围内的 3 个(或可能更多)参数生成一系列值。

对于给定的输入,该值必须始终相同,但在上下边界之间具有公平分布,以便显得随机。

例如:

function (1, 1, 1) = 423

function (1, 1, 2) = 716

function (1, 2, 1) = 112

这些必须相当快地生成,我的意思是我应该能够在网页加载期间生成100-200 ,而没有明显的延迟。

该方法必须在 C# 和 JavaScript 中都可以使用,否则我可能会使用 CRC32 或 MD5 哈希算法。

如果有帮助,这将用作程序生成例程的一部分。

我之前曾问过这个问题,但我认为我的解释质量差让我失望。

如果措辞不当,我深表歉意。如果是这样,请告诉我,我会尝试进一步解释。

非常感谢您的帮助。

4

3 回答 3

5

这是一个:

function sequence(x, y, z) {
    return Math.abs(441*x-311*y+293*z) % 1000;
}

它甚至可以从您的示例中生成输出!

于 2013-08-12T13:35:51.073 回答
1

使用Wiki中的 Marsaglia 生成器

public class SimpleMarsagliaRandom
{
    private const uint original_w = 1023;
    private uint m_w = original_w;    /* must not be zero */
    private uint m_z = 0;    /* must not be zero, initialized by the constructor */

    public SimpleMarsagliaRandom()
    {
        this.init(666);
    }

    public void init(uint z)
    {
        this.m_w = original_w;
        this.m_z = z;
    }

    public uint get_random()
    {
        this.m_z = 36969 * (this.m_z & 65535) + (this.m_z >> 16);
        this.m_w = 18000 * (this.m_w & 65535) + (this.m_w >> 16);
        return (this.m_z << 16) + this.m_w;  /* 32-bit result */
    }

    public uint get_random(uint min, uint max)
    {
        // max excluded
        uint num = max - min;
        return (this.get_random() % num) + min;
    }
}

simpleMarsagliaRandom = function() 
{
    var original_w = 1023 >>> 0;
    var m_w = 0, m_z = 0;

    this.init = function(z)
    {
        m_w = original_w;
        m_z = z >>> 0;
    };

    this.init(666);

    var internalRandom = function()
    {
        m_z = (36969 * (m_z & 65535) + (m_z >>> 16)) >>> 0;
        m_w = (18000 * (m_w & 65535) + (m_w >>> 16)) >>> 0;
        return (((m_z << 16) >>> 0) + m_w) >>> 0;  /* 32-bit result */
    };

    this.get_random = function(min, max)
    {
        if (arguments.length < 2)
        {
            return internalRandom();
        }

        var num = ((max >>> 0) - (min >>> 0)) >>> 0;
        return ((internalRandom() % num) + min) >>> 0;
    }    
};

在Javascript中,所有的>>>都是强制数字uint

完全未经测试

请注意,get_random将数字从 x 变为 y 所做的事情是错误的。低数字会比高数字发生的次数多一点。举个例子:假设你有一个标准的 6 面骰子。你滚动它,你得到1-6。现在假设您在其上打印数字 0-5。你滚动它,你得到0-5。没问题。但是您需要 0-3 范围内的数字。所以你滚动 % 3 ......所以我们有:

rolled => rolled % 3
0 => 0, 
1 => 1, 
2 => 2, 
3 => 0, 
4 => 1, 
5 => 2, 
6 => 0. 

0 结果更常见。

C# 版本的 Ideone:http: //ideone.com/VQudcV
Javascript 版本的 JSFiddle:http: //jsfiddle.net/dqayk/

于 2013-08-12T13:39:45.843 回答
0

您应该能够在 C# 和 JS 中使用 MD5 散列。

在 C# 中:

int Hash(params int[] values)
{
    System.Security.Cryptography.MD5 hasher = MD5.Create();
    string valuesAsString = string.Join(",", values);
    var hash = hasher.ComputeHash(Encoding.UTF8.GetBytes(valuesAsString));
    var hashAsInt = BitConverter.ToInt32(hash, 0);
    return Math.Abs(hashAsInt % 1000);
}

在 JS 中,使用一些 MD5 算法(例如jshash)实现相同的方法

于 2013-08-12T13:40:30.323 回答