Var help | Sololearn: Learn to code for FREE!


Var help

Can you make a variation go radomly between numbers? Example (this is very wrong but i want you to get the picture): var a = 10-20; document.getElementById(“ID”).innerHTML = a; I know this is wrong but is it possible to do it even in a longer way of coding it? If its not possible its ok to tell me.

12/30/2017 2:23:40 AM


9 Answers

New Answer


Something like this will do the trick. May not be the simplest way, but I'm still learning :) Note: I have set an infinite interval going. You can stop it using clearInterval(changeIt); within an 'if' statement within the changeIt function.


The standart formula for that is a = Math.floor(min+(max-min)*Math.random());


Var a = Math.floor(Math.random() * 11) + 10; this will generate a random integer value, between 10 and 20 (including 10 and 20)


@Will Cavalcanti, your solution doesn't include 10 What you might want is floor(rand()*11)+10) instead


I'm not absolutely certain to get what you mean, do you want a randomly chosen number between, here, 10 and 20?


yup my mistake


You're welcome! :')


Thanks everyone!


So the value of that is now random between that number?