最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - random number between 0 - 8 JavaScript - Stack Overflow

programmeradmin0浏览0评论

I am trying to generate 40 random numbers between 0 to 8

This is my code --

<body>
    <h1>This is a page for randomize number between 0 - 8 </h1> <br />

<button onclick="randomize()">Random number</button>

<p id="Output"></p>

<script>
function randomize() {

    var arr = []
while(arr.length < 40){
  var randomnumber=Math.floor((Math.random() * 8) + 1)
  arr.push(randomnumber) 
}
document.getElementById("Output").innerHTML = arr;
}
</script> 
</body>

I am expecting 40 random numbers to be Output like --

4
4
5
7
8
1
5
7

What I am doing wrong can someone correct me please and also I want the result to be outputted by the next line !

Can someone help !

I am trying to generate 40 random numbers between 0 to 8

This is my code --

<body>
    <h1>This is a page for randomize number between 0 - 8 </h1> <br />

<button onclick="randomize()">Random number</button>

<p id="Output"></p>

<script>
function randomize() {

    var arr = []
while(arr.length < 40){
  var randomnumber=Math.floor((Math.random() * 8) + 1)
  arr.push(randomnumber) 
}
document.getElementById("Output").innerHTML = arr;
}
</script> 
</body>

I am expecting 40 random numbers to be Output like --

4
4
5
7
8
1
5
7

What I am doing wrong can someone correct me please and also I want the result to be outputted by the next line !

Can someone help !

Share Improve this question asked Feb 25, 2016 at 1:02 DanDan 3432 gold badges6 silver badges16 bronze badges 1
  • What does your code output at the moment? – Blue Boy Commented Feb 25, 2016 at 1:04
Add a ment  | 

3 Answers 3

Reset to default 2

Your code already works, just ad line breaks:

<body>
<h1>This is a page for randomize number between 0 - 8 </h1> <br />

<button onclick="randomize()">Random number</button>

<p id="Output"></p>

<script>
function randomize() {

    var arr = []
    while(arr.length < 40){
        var randomnumber=Math.floor((Math.random() * 9)) // 0-8 is 9 numbers
        arr.push(randomnumber) 
    }
    document.getElementById("Output").innerHTML = arr.join('<br>'); // Add line break between numbers

}
</script> 
</body>

EDIT: changed random multiplier from 8 to 9

a) If you multiply by 8, you are getting numbers from 0 to 7.

b) .innerHTML = arr.join('<br>') should give you the output you want.

I think you need to look into your random calculation and use .join() to make the number in separate lines.

1) The random formulate should be Math.floor(Math.random()*(1+High-Low))+Low;. Since you multiply Math.random() with 8, you will only get 7 as maximum. However you're adding +1 after random, you will still get 8 as maximum number but the minimum bees 1. You're genearating 1 to 8 now.

2) If you need to echo each number in a new line, you need to join the array with <br/>. E.g. document.getElementIdBy('Output').innerHtml = arr.join('<br/>').

发布评论

评论列表(0)

  1. 暂无评论