Var help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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.

30th Dec 2017, 2:23 AM
JOHNTHEBUN
JOHNTHEBUN - avatar
9 Answers
+ 7
Something like this will do the trick. May not be the simplest way, but I'm still learning :) https://code.sololearn.com/W3QNFabgpBP7/#html Note: I have set an infinite interval going. You can stop it using clearInterval(changeIt); within an 'if' statement within the changeIt function.
30th Dec 2017, 2:42 AM
Duncan
Duncan - avatar
+ 6
The standart formula for that is a = Math.floor(min+(max-min)*Math.random());
30th Dec 2017, 2:36 AM
Dapper Mink
Dapper Mink - avatar
+ 5
Var a = Math.floor(Math.random() * 11) + 10; this will generate a random integer value, between 10 and 20 (including 10 and 20)
30th Dec 2017, 2:34 AM
Welliton Malta
Welliton Malta - avatar
+ 4
@Will Cavalcanti, your solution doesn't include 10 What you might want is floor(rand()*11)+10) instead
30th Dec 2017, 2:38 AM
Dapper Mink
Dapper Mink - avatar
+ 3
I'm not absolutely certain to get what you mean, do you want a randomly chosen number between, here, 10 and 20?
30th Dec 2017, 2:25 AM
Dapper Mink
Dapper Mink - avatar
+ 1
yup my mistake
30th Dec 2017, 2:40 AM
Welliton Malta
Welliton Malta - avatar
+ 1
You're welcome! :')
30th Dec 2017, 2:42 AM
Dapper Mink
Dapper Mink - avatar
+ 1
Thanks everyone!
31st Dec 2017, 12:28 AM
JOHNTHEBUN
JOHNTHEBUN - avatar
0
So the value of that is now random between that number?
30th Dec 2017, 2:38 AM
JOHNTHEBUN
JOHNTHEBUN - avatar