-1

So, say I have a number, c, and when I click a button it randomly increases by another number, b, which is any number between 1-10. I have tried math.floor(math.random()) and document-getElementById('#id').innerhtml="". Please help, preferably save c as a variable. Thanks.

user2936538
  • 55
  • 1
  • 2
  • 7
  • 1
    Show us the code that you have. – Tyler Jan 07 '14 at 22:18
  • possible duplicate of [Generate random value between two numbers in Javascript](http://stackoverflow.com/questions/4959975/generate-random-value-between-two-numbers-in-javascript) – Niet the Dark Absol Jan 07 '14 at 22:19
  • I've suggested you an implementation for that random number generator functionality, but update your answer with more code so we can help you out. – João Pinho Jan 07 '14 at 22:39

1 Answers1

0

You need to use

Math.floor(1 + Math.random() * 10)

Math.random() generates a random number between 0 and 1 (except) [0,1[. Here further info about Math.random(): http://www.w3schools.com/jsref/jsref_random.asp

var c = 120;

window.addEventListener("load",function(){

   document.getElementById('btnRandom').addEventListener("click",function(){

        //this code goes inside click event function
        var b = c + Math.floor(1 + Math.random() * 10);
        document.getElementById('results').innerHTML= "random: " + b;

   }, false);

}, false);


<div id="results"></div>
<input type="button" id="btnRandom" value="Random"/>

And a JSFiddle: http://jsfiddle.net/TRSZj/

Regards.

João Pinho
  • 3,587
  • 1
  • 17
  • 29