0

I try to send a random number into a tag in my script to test how my page will react.

So here's what i've got.

<script>
    display_number = 1.5684651 ;
    display = display_number;
    function DIS_EN() {
        var num = display;
        var n = num.toFixed(2);
        document.getElementById("display_value").innerHTML = n;
    }
</script>

Rignt now the display_number is fixed to 1.5684651, what I want is to get a random number in the display_number tag. This information will come from an external page and it will vary so that's why I want a random number.

How can I do that? I know I can use a Math.random to generate a random number but I don't know how to show it in my display_number tag...

EDIT:

One more thing: I want it to change by itself... I tried setInterval(function(){Math.random},2000), but it doesn't seem to work.

EDIT 2:

Here's my latest code, with the setInterval in it

<script>
    setInterval(function(){display_number = Math.floor((Math.random() * 1.0E+10) + 1);}, 2000);
    display = display_number;
    function DIS_EN() {
        var num = display;
        var n = num.toFixed(2);
        document.getElementById("display_value").innerHTML = n;
    }
</script>

I get a new value everytime I refresh the page only when I add the setInterval I get a blank page...

  • See http://stackoverflow.com/questions/1527803/generating-random-numbers-in-javascript-in-a-specific-range – soktinpk Jun 05 '14 at 15:17

1 Answers1

0

See random() from the w3schools:

// Returns a number from 0 to 1
display_number = Math.floor((Math.random() * 10) + 1); // Returns 1 - 10

To make this go by itself, just wrap the whole line inside of your setInterval code:

setInterval(function() {
    display_number = Math.floor((Math.random() * 10) + 1); // Returns 1 - 10
    // Update your UI here...
}, 2000);
Uxonith
  • 1,602
  • 1
  • 13
  • 16
  • 1
    Please find a more reliable source. Possible https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random? – putvande Jun 05 '14 at 15:19
  • it's almost it, I just need it to change by itself without refreshing the page... Any idea? – user3711700 Jun 05 '14 at 15:27
  • I tried to do as you said with the `setInterval`, but now I only have a blank page. Is there another way? – user3711700 Jun 05 '14 at 15:41
  • What's your updated code? It's hard to say why your page would be blank after what I gave you. I'm assuming however you "Updated your UI" would be causing the issue – Uxonith Jun 05 '14 at 15:43
  • This code is generating an error: Uncaught ReferenceError: display is not defined (anonymous function) – user3711700 Jun 05 '14 at 17:43
  • Any luck with this yet? Do you declare your `display` and `display_number` outside the scope of the function? I know global variables are bad, but before setInterval in your code, you could try to declare `var display, dislpay_number;` Inside your `setInterval`, that `display_number` may be out of scope once you assign `display = display_number;`. – Uxonith Jun 06 '14 at 16:39
  • It worked! I just had to put every variable into the function `DIS_EN()` and add `setInterval(function(){DIS_EN()},2000;` in my script, instead of leaving `display` and `display_number` above the function, like this: `function DIS_EN(){ var display_number = Math.random; var display = display_number; [...]` Although thanks for the advice – user3711700 Jun 09 '14 at 13:49