Javascript JS 生成随机布尔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36756331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JS generate random boolean
提问by Ben
Simple question, but I'm interested in the nuances here.
简单的问题,但我对这里的细微差别感兴趣。
I'm generating random booleans using the following method I came up with myself:
我正在使用我自己想出的以下方法生成随机布尔值:
const rand = Boolean(Math.round(Math.random()));
Whenever random()
shows up, it seems there's always a pitfall - it's not truly random, it's compromised by something or other, etc. So, I'd like to know:
每当random()
出现时,似乎总会有一个陷阱 - 它不是真正随机的,它受到某些东西或其他东西的影响,等等。所以,我想知道:
a) Is the above the best-practice way to do it?
a) 以上是最佳实践方法吗?
b) Am I overthinking things?
b) 我是不是想多了?
c) Am I underthinking things?
c) 我是不是想得太少了?
d) Is there a better/faster/elegant-er way I don't know of?
d) 有没有我不知道的更好/更快/更优雅的方式?
(Also somewhat interested if B and C are mutually exclusive.)
(如果 B 和 C 相互排斥,也有点兴趣。)
Update
更新
If it makes a difference, I'm using this for movement of an AI character.
如果它有所作为,我将使用它来移动 AI 角色。
回答by Kelvin
Technically, the code looks fine, but just a bit too complex. You can compare "Math.random()" to "0.5" directly, as the range of Math.random() is [0, 1). You can divide the range into [0, 0.5) and [0.5, 1).
从技术上讲,代码看起来不错,但有点太复杂了。你可以直接比较 "Math.random()" 和 "0.5",因为 Math.random() 的范围是 [0, 1)。您可以将范围划分为 [0, 0.5) 和 [0.5, 1)。
var random_boolean = Math.random() >= 0.5;
// Example
console.log(Math.random() >= 0.1) // %90 probability of get "true"
console.log(Math.random() >= 0.4) // %60 probability of get "true"
console.log(Math.random() >= 0.5) // %50 probability of get "true"
console.log(Math.random() >= 0.8) // %20 probability of get "true"
console.log(Math.random() >= 0.9) // %10 probability of get "true"
回答by Alexander O'Mara
For a more cryptographically secure value, you can use crypto.getRandomValues
in modern browsers.
对于更加密安全的值,您可以crypto.getRandomValues
在现代浏览器中使用。
Sample:
样本:
var randomBool = (function() {
var a = new Uint8Array(1);
return function() {
crypto.getRandomValues(a);
return a[0] > 127;
};
})();
var trues = 0;
var falses = 0;
for (var i = 0; i < 255; i++) {
if (randomBool()) {
trues++;
}
else {
falses++;
}
}
document.body.innerText = 'true: ' + trues + ', false: ' + falses;
Note that the crypto
object is a DOM API, so it's not available in Node, but there is a similar API for Node.
请注意,该crypto
对象是一个 DOM API,因此它在 Node 中不可用,但是 Node有一个类似的 API。
回答by user12066722
!Math.round(Math.random());
--------------
--------------
回答by Arthur Khazbs
Impressed a lot by Kelvin's answerI would like to suggest a fairly similar but slightly enhanced solution.
开尔文的回答给我留下了深刻的印象,我想建议一个相当相似但稍微增强的解决方案。
var randomBoolean = Math.random() < 0.5;
This solution is a bit more obvious to read, because the number on the right-hand side of <
tells you the probability of getting true
rather than of getting false
, which is more natural to comprehend. Also <
is one symbol shorter than >=
;
这个解决方案读起来更明显一点,因为右边的数字<
告诉你得到true
而不是得到的概率false
,这更容易理解。也<
比>=
;短一个符号
回答by Alex
How about this one?
这个怎么样?
return Math.round((Math.random() * 1) + 0) === 0;
回答by bFunc
Answer of Alexander O'Mara
just adding node code snippet
只需添加节点代码片段
const crypto = require('crypto');
const randomBool = (function () {
let a = new Uint8Array(1);
return function () {
crypto.randomFillSync(a);
return a[0] > 127;
};
})();
let trues = 0;
let falses = 0;
for (let i = 0; i < 100; i++) {
if (randomBool()) {
trues++;
}
else {
falses++;
}
}
console.log('true: ' + trues + ', false: ' + falses);