我有一个随机生成的sha256哈希,并希望基于此哈希生成介于0和1之间的数字。我该怎么做呢?



var hash = crypto.createHmac("sha256", config.server_seed).update(this.roundID).digest("hex");

var randomNumber = parseInt(hash, 16) // am I along the right tracks here? 

最佳答案

您处在正确的轨道上,只需将randomNumber除以SHA256哈希空间大小,即Math.pow(2, 256)

var hash = crypto.createHmac("sha256", config.server_seed).update(this.roundID).digest("hex");

var randomNumber = parseInt(hash, 16) / Math.pow(2, 256);


虽然我不确定您为什么要这么做...

10-06 04:22
查看更多