- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望两个客户端使用相同的“种子”通过输入时间戳或长数字来生成相同的数字,并像使用 random() 一样返回 float 。
例如,使用函数:hasher(String seeds, long input);
hasher("StackOverflow", 1000000010); //returns 0.57217329503213..
hasher("StackOverflow", 1000000010); //returns 0.57217329503213...
hasher("StackOverflowz", 1000000010); //returns 0.15689784654554...
hasher("StackOverflow", 97494849465); //returns 0.456944151561...
它确实不应该是安全的或私密的,而只是足够随机。我认为我可以使用位操作,但我绝不是散列或位操作方面的专家。
我意识到种子与组合种子和输入相比几乎是多余的,但我认为了解在不影响可靠性的情况下实现种子的最佳方法会有所帮助。有什么想法吗?
谢谢。
最佳答案
我做了一个小实现,但要注意 JavaScript 与我的主要语言相去甚远。该代码需要 CryptoJS 来实现哈希功能。
function hasher(a, b) {
hash = fromHex(CryptoJS.enc.Hex.stringify(CryptoJS.SHA256(a + b)));
// warning: only about 32 bit precision
hashAsDouble = intFromBytes(hash.slice(0, 4)) * (1.0 / 4294967296.0);
return hashAsDouble;
}
function fromHex(hex) {
a = [];
for (var i = 0; i < hex.length; i += 2) {
a.push("0x" + hex.substr(i, 2));
}
return a;
}
function intFromBytes(x) {
if (x.length != 4) {
return null;
}
var val = 0;
for (var i = 0; i < x.length; i++) {
val = val << 8;
val = val + (x[i] & 0xFF);
}
return toUint32(val);
}
function modulo(a, b) {
return a - Math.floor(a / b) * b;
}
function toUint32(x) {
return modulo(toInteger(x), Math.pow(2, 32));
}
function toInteger(x) {
x = Number(x);
return x < 0 ? Math.ceil(x) : Math.floor(x);
}
关于javascript - 类似 PRNG 的种子哈希函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24941475/
Sequelize 中有没有办法添加另一列,然后用另一列的内容填充它? 最佳答案 您可以使用迁移来做到这一点。 就像是 queryInterface.addColumn( 'MyAwesomeTa
如何计算info_hash参数?又名对应于信息字典的哈希?? 来自官方规范: info_hash The 20 byte sha1 hash of the bencoded form of the i
是否可以直接从数据库或服务等将 URL 获取到 Nutch。我对从数据库或服务获取数据并将其写入 Seed.txt 的方式不感兴趣. 最佳答案 没有。这不能直接使用默认的 nutch 代码库来完成。需
MessageDigest 类实现了 SHA-1 算法(以及许多其他算法)。 SHA-1 算法允许使用不同的“种子”或初始摘要。参见 SHA-1 Psuedocode 算法初始化变量,或种子: Ini
我想创建一个应用程序,其中登录密码可以作为伪随机数生成器的种子以重新创建加密 key 。然后,该加密 key 将用于加密发送到应用程序数据库和从应用程序数据库发送的所有数据,使用户数据甚至主机都无法访
这个问题在这里已经有了答案: Recommended way to initialize srand? (15 个答案) 关闭 8 年前。 使用 srand(time(NULL))似乎过于确定性。例
我在获取要在我的自定义数据库初始值设定项上调用的 Seed 方法时遇到问题。我正在使用 EF 5.0 并具有以下代码: public static class MyDatabase { pub
是否可以像在 Rails 中那样“播种”数据库?我想将种子与图像对象管理器结合使用,以便我可以按标题获取记录。 最佳答案 根据您对 Ingo 的回答留下的评论,您想将 requireDefaultRe
我现在设置了一个应用程序来使用 EF6 代码优先迁移。我使用 Add-Migration 的标准工作流程,然后在控制台中使用 Update-Database。我在本地以及我们的开发环境中使用 Migr
如果 Name 返回然后删除 first name Name john Age 30 Name Alice Name Travis Age 12 Name Monty Name Hannah 期望的输
在迁移完成后,是否可以在我的迁移中放入一些东西来自动为表播种测试数据? 或者您必须单独播种? 最佳答案 您可以使用 --seed 选项调用 migrate:refresh 以在迁移完成后自动播种: p
我正在尝试使用不同的种子生成 scipy.stats.pareto.rvs(b, loc=0, scale=1, size=1)。 在 numpy 中,我们可以使用 numpy.random.seed
我的种子有问题。这是我的表结构: 1.Complaints: Schema::create('complaints', function (Blueprint $table) {
我在使用数据库初始化程序时遇到问题 - 从未调用过种子方法。类似的代码在另一个项目中工作,所以我很困惑为什么他们这次不工作。 这是我的代码: RecipeContext.cs public c
我正在尝试做一些我认为非常简单的事情,只需使用 RAND 创建 0-1 之间的随机数,并将其分配给十进制变量。但每次我在 MySQL 中运行代码时,它都会返回零! 参见下面的代码: DELIMITER
我有一个问题...... 这里我们得到了一个二维字节数组: byte[][] duengonMap = new byte[500][500]; 因为我想将它从客户端发送到服务器或者相反,我需要将其放入
我尝试在我的计算机上运行 Angular-seed(Windows 10,上次更新)https://github.com/angular/angular-seed 。网络工作正常,但我的 Protra
我有一个随机过程的分布式过程。因此,我使用 numpy.random.RandomState 来播种数字。问题是我必须在包装器中使用另一个 numpy.random 函数。现在我失去了种子的再现性,因
我需要确保我程序中的所有随机性都是完全可复制的。我应该在哪里调用 random.seed()? 我认为它应该在我的 main.py 模块中,但它导入了碰巧使用随机函数的其他模块。 我可以仔细浏览我的导
首先尝试使用 Entity Framework 和代码在 ASP.NET 网络应用程序中植入数据。我将这段代码放在 Configuration.cs 文件的 Seed() 方法中。现在,我正在处理解决
我是一名优秀的程序员,十分优秀!