admin管理员组文章数量:1429953
// Storing array and targeting HTML elements.
var array = new Uint8Array(1),
output = document.getElementById('output'),
btn = document.getElementById('btn');
btn.onclick = function() {
// Get random values from the array.
number = window.crypto.getRandomValues(array);
// Show value on the front-end.
output.innerHTML = number[0];
};
h1, button {
text-align: center;
font-family: sans-serif;
}
button {
padding: 20px;
display: block;
margin: 0 auto;
width: 175px;
border: solid 5px #000;
border-radius: 2px;
background: none;
font-size: 20px;
cursor: pointer;
text-transform: uppercase;
}
<h1 id="output">Click to go random.</h1>
<button id="btn">Randomize</button>
// Storing array and targeting HTML elements.
var array = new Uint8Array(1),
output = document.getElementById('output'),
btn = document.getElementById('btn');
btn.onclick = function() {
// Get random values from the array.
number = window.crypto.getRandomValues(array);
// Show value on the front-end.
output.innerHTML = number[0];
};
h1, button {
text-align: center;
font-family: sans-serif;
}
button {
padding: 20px;
display: block;
margin: 0 auto;
width: 175px;
border: solid 5px #000;
border-radius: 2px;
background: none;
font-size: 20px;
cursor: pointer;
text-transform: uppercase;
}
<h1 id="output">Click to go random.</h1>
<button id="btn">Randomize</button>
I've been experimenting with UInt8Array()
method in Javascript to create random number. When used with window.crypto.getRandomValues()
, it works even better than Math.random()
.
However I would like to know what's the max value this method could generate, so I was wondering if someone more experienced could give this information and perhaps explain me how this works and how this maximum value is defined. Also, can I use these numbers and store them in a variable?
Share Improve this question edited Dec 16, 2015 at 21:32 csalmeida asked Mar 6, 2015 at 22:09 csalmeidacsalmeida 62810 silver badges30 bronze badges 3- 1 I'm really curious and hope you'll answer this question: How in the world are you using a UInt8Array method to generate random numbers? That is some serious off-label use. – szupie Commented Mar 6, 2015 at 22:19
- 2 I was about to ask the same question, would you share a jsfiddle? – Gus Ortiz Commented Mar 6, 2015 at 22:19
-
@szupie @Gus Ortiz I apologize, only now I've seen your questions. I've edited the question and added a jsfiddle as requested to explain that. Probably there are easier ways to do it but this is the way that I've found when I want a number bigger than
255
. – csalmeida Commented Dec 16, 2015 at 21:35
1 Answer
Reset to default 4The definition of the method says:
The Uint8Array typed array represents an array of 8-bit unsigned integers. The contents are initialized to 0 ..
This means that the highest value is 2^8 - 1 meaning 255.
Calculated in JS as Math.pow(2, 8);
本文标签: javascriptWhat is UInt8Array() max valueStack Overflow
版权声明:本文标题:javascript - What is UInt8Array() max value? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745557522a2663273.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论