javascript中的随机数 [英] Random numbers in javascript
本文介绍了javascript中的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以告诉我是否有办法在阵列中生成随机数?
以下是我的规格
随机值应为1到25
所有值都应存储到数组中而不重复
i got生成随机数的代码,
Can anyone please tell me if there is a way to generate random numbers into an array?
Below are my specifications
Random values should be from 1 to 25
All the values should be stored into an array without repeating
i got a code which generates random numbers,
Math.floor(Math.random() * (25 - 1 + 1)) + 1
但是它正在生成已经生成的值(默认行为)。
but it is generating already generated values(default behaviour).
推荐答案
这里你可以看到1到25的随机数。如果你看看更多然后更改25文字并添加你想要的内容。
Here you can see random number for 1 to 25. if you look more then change "25" text and add what you want.
<html>
<body>
Click the button to display a random number between 1 and 10.
<button >Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = Math.floor((Math.random() * 25) + 1);
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
将数字1-25存储在一个数组中,然后将它们随机播放。
Store the numbers 1-25 in an array, then shuffle them.
这篇关于javascript中的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文