0

Recently i've been trying to do a repetitive random number in JS, but the problem is that always i have to reload the page to get a new random number, even i try to show it multiple times but the problem is that the number is the same until i refresh the page.

<?php

<script type="text/javascript" charset="utf-8">
    function getNumber(){
        var number = Math.random()
        return number;
    }

    document.write(getNumber()); // NUMBER 1
    document.write(getNumber()); // SAME AS NUMBER 1
    document.write(getNumber()); // SAME AS NUMBER 1
    document.write(getNumber()); // SAME AS NUMBER 1
    document.write(getNumber()); // SAME AS NUMBER 1

</script>

?>
Brett Zamir
  • 12,481
  • 5
  • 45
  • 68
kaZep
  • 21
  • 7
  • Why are there PHP brackets surrounding your JavaScript? – Jay Blanchard Jun 26 '15 at 20:52
  • 1
    It works properly for me: http://codepen.io/barmar/pen/aOEZvP – Barmar Jun 26 '15 at 20:59
  • 1
    Is there anything in your real code that you haven't shown us? Your code should work. One thing to notice: you're not putting anything between the numbers, so they're all crushed together on the same line. Maybe you couldn't tell that they were different numbers? – Barmar Jun 26 '15 at 21:19

2 Answers2

0

Math.random() returns a number between 0 and 1. Please read the docs for more information.

Rather than using document.write just update the content of your element like this:

function getNumber(){
    var number = Math.random()
    return number;
}

var div = document.getElementById('result');
div.innerHTML = getNumber();
Jay Blanchard
  • 32,731
  • 15
  • 70
  • 112
  • 2
    So why doesn't he get 5 different numbers between 0 and 1? – Barmar Jun 26 '15 at 20:56
  • That's what i'm actually asking :/ i'm not looking to take a range of numbers, i just want different numbers. – kaZep Jun 26 '15 at 20:57
  • Repeated 1's *are* random. – Jay Blanchard Jun 26 '15 at 20:59
  • I tried the range method, it's absolutely the same thing. – kaZep Jun 26 '15 at 21:00
  • 1
    Your code probably rounds up or down every value between 0 and 1 so it appears to be the same value, but has Barmar posted under your question, it works – Alex Jun 26 '15 at 21:00
  • @JayBlanchard Why would he get repeated 1's? He's not converting the result ot an integer. – Barmar Jun 26 '15 at 21:17
  • Not sure @Barmar, because I cannot replicate the problem. The number is being rounded somewhere, but it isn't `document.write` because as you have shown (and I have tested) `document.write` doesn't impose rounding, [as far as I know](http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice). – Jay Blanchard Jun 26 '15 at 21:21
0

I think it might be because you are using document.write. Use console.log instead and take a look at the console.

console.log(getNumber());
console.log(getNumber());

If you want your results in the browser get a reference to an element, or make and append one.

  • It works with `document.write()` in my codepen. Why do you think it matters what function you're calling? – Barmar Jun 26 '15 at 21:16
  • I am using jsfiddle, it is not allowed there. Also if document.write is called after the document is loaded it will write over the whole page. That may not be the case here, but it still differs from console.log big time. I generally avoid using documenbt.write, there may be only a few cases where I might choose to use it. I was in a rush when I posted this, but I also now see that the script tag is inside a php bracket, that's not php there. – Dustin John Pfister Jun 26 '15 at 22:53
  • Yes, there are good reasons not to use `document.write()` (I had to do my demo on codepen because it didn't work on jsfiddle). But they don't have anything to do with whether you get the same number repeated. The PHP bracket in the question is obviously a copying error, or he wouldn't be getting anything at all. – Barmar Jun 26 '15 at 22:57
  • I guess so, I'm not a PHP developer but now that I think about it if you where to place a script tag inside a PHP bracket I would image that would cause an error. I just noticed that about document.write in my own testing working on the example offline via the file:// protocol, guess the reason why I am not aware of this is because I never use it. – Dustin John Pfister Jun 26 '15 at 23:10