【问题标题】:Matching a classic ASP random number with a C# random number将经典 ASP 随机数与 C# 随机数匹配
【发布时间】:2011-07-09 22:29:25
【问题描述】:

有点奇怪的问题。我有一个网站,其中一些页面使用经典 ASP,其他页面使用 ASP.net。

我有一个缓存他们的 gravatar 图像的脚本。它托管在无 cookie 域上,位于以下位置之一:

http://static1.scirra.net
http://static2.scirra.net
http://static3.scirra.net
http://static4.scirra.net

当一个页面在我的 ASP.net 站点上请求一个 gravatar 时,它会通过这个函数将它随机分发到一个静态服务器:

/// <summary>
/// Returns the static url for gravatar
/// </summary>
public static string GetGravatarURL(string Hash, int Size, int AuthorID)
{
    Random rndNum = new Random(AuthorID);
    int ServerID = rndNum.Next(0, 4)+1;

    string R = "//static" + ServerID.ToString() + ".scirra.net/avatars/" + Size + "/" + Hash + ".png";
    return R;
}

我的网站Classic ASP部分的功能是:

function ShowGravatar(Hash, AuthorID)

    Dim ServerID

    Randomize(AuthorID)
    ServerID = Int((Rnd * 4) + 1)

    ShowGravatar = "//static" & ServerID & ".scirra.net/avatars/" & intGravatarSize & "/" & Hash & ".png"

end function

它工作正常,它在用户 ID 上播种,然后为他们分配一个静态服务器来为他们的头像提供服务。唯一的问题是,C# 和 Classic ASP RNG 的输出结果不同!这不是缓存的最佳选择,因为同一图像最多可用于 2 个不同的域。

有什么简单的方法吗?

【问题讨论】:

  • 作为旁注:请不要告诉人们您不会发布他们的电子邮件,然后使用 gravatar。许多网站都以这种方式欺骗用户。
  • @Codeinchaos,你是什么意思?哈希基本上是匿名的,不是吗?
  • 许多电子邮件地址是低熵的,可以被猜到。例如,我设法从 gravatar 哈希中获取了超过 20% 的 SO 电子邮件地址,但随着计算能力和更好的电子邮件生成算法,更高的百分比应该是可能的。

标签: c# caching random asp-classic


【解决方案1】:

返回可预测值的随机数生成器称为哈希 - 可预测随机性在随机数生成器中一点也不酷:-)

所以,用一些散列函数替换对 rand 的调用,一切就绪。发挥你的想象力:散列函数可以是简单的东西,就像 authorid 的 crc 的模 4。

【讨论】:

    【解决方案2】:

    为什么不直接使用 gravatar 哈希来确定服务器呢?例如,您可以取 gravatar 哈希模 4 的第一个字符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-09
      • 1970-01-01
      • 1970-01-01
      • 2019-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      相关资源
      最近更新 更多