+ 2

Random Function in JavaScript

How do I generate a random function in JavaScript?

15th May 2020, 6:51 AM
Unrealistic Engineer
Unrealistic Engineer - avatar
48 Answers
+ 6
Math.random() returns a random number between 0 (inclusive),  and 1 (exclusive). Math.random() used with Math.floor() can be used to return random integers. For example Math.floor(Math.random() * 10); Returns random integer from 0 to 9
17th May 2020, 6:52 AM
Bhavya
Bhavya - avatar
+ 3
f_arr = [add1 ,add2 ,add3 ,add4 ,add5 ,add6 ,add7 ,add8 ,add9 ]; function comp(){ if(f_arr.length > 0){ var i = Math.floor(Math.random()*f_arr.length); f_arr[i](); } }
15th May 2020, 9:55 AM
Mohsin Uddin
Mohsin Uddin - avatar
+ 2
Lets say for example that I want to trigger a random animation when the page is loaded. How do I do that?
15th May 2020, 7:24 AM
Unrealistic Engineer
Unrealistic Engineer - avatar
+ 2
Unrealistic Engineer i will make a project I think it's the same as you say
15th May 2020, 7:36 AM
ycsvenom
ycsvenom - avatar
+ 2
Unrealistic Engineer look to this If you are using laptop just hover the box If you are using mobile just click the box https://code.sololearn.com/WW1wYUioHnr0/?ref=app
15th May 2020, 8:09 AM
ycsvenom
ycsvenom - avatar
15th May 2020, 10:02 AM
ycsvenom
ycsvenom - avatar
15th May 2020, 3:29 PM
narayanaprasad
narayanaprasad - avatar
+ 2
Math.floor(Math.random() * 10);     // returns a random integer from 0 to 9 Math.floor(Math.random() * 10) + 1;  // returns a random integer from 1 to 10 Math.floor(Math.random() * 1000000000); //generates a 9 digit random number I hope this helps you Unrealistic Engineer
15th May 2020, 5:28 PM
Nikhil Maroju
Nikhil Maroju - avatar
+ 2
㋛︎ᴥ︎𓃰︎
16th May 2020, 7:22 AM
|¦|¦×Kristina×|¦|¦
|¦|¦×Kristina×|¦|¦ - avatar
+ 2
Lloyd L Conley is right.✅✔️
17th May 2020, 2:11 AM
narayanaprasad
narayanaprasad - avatar
+ 1
Thank you, sounds great!
15th May 2020, 7:42 AM
Unrealistic Engineer
Unrealistic Engineer - avatar
+ 1
It generates a random background color
15th May 2020, 8:10 AM
ycsvenom
ycsvenom - avatar
+ 1
Thank you, YCS-VENOM! What if I wanted to trigger a random animation event on click and the options are: 1. The block moves to the left 2. The block moves up 3. The block moves to the right?
15th May 2020, 8:54 AM
Unrealistic Engineer
Unrealistic Engineer - avatar
+ 1
Kelvin Paul I'm trying to create a random animation event.
15th May 2020, 9:04 AM
Unrealistic Engineer
Unrealistic Engineer - avatar
+ 1
Unrealistic Engineer You mean when click the box it might go to any of directions?
15th May 2020, 9:08 AM
ycsvenom
ycsvenom - avatar
+ 1
YCS-Venom Yes. :)
15th May 2020, 9:10 AM
Unrealistic Engineer
Unrealistic Engineer - avatar
+ 1
And the movement is const or random too? Unrealistic Engineer
15th May 2020, 9:23 AM
ycsvenom
ycsvenom - avatar
+ 1
YCS-Venom Movement can be const. Three options: Left, up and down.
15th May 2020, 9:45 AM
Unrealistic Engineer
Unrealistic Engineer - avatar
+ 1
Thank you! Yes, it's nearly perfect. The only thing missing is continuous movement when the box moves to different directions.
15th May 2020, 10:14 AM
Unrealistic Engineer
Unrealistic Engineer - avatar
+ 1
Unrealistic Engineer just a minute I will do it
15th May 2020, 10:15 AM
ycsvenom
ycsvenom - avatar