+ 2
Random Function in JavaScript
How do I generate a random function in JavaScript?
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
+ 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]();
}
}
+ 2
Lets say for example that I want to trigger a random animation when the page is loaded. How do I do that?
+ 2
Unrealistic Engineer i will make a project
I think it's the same as you say
+ 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
+ 2
Unrealistic Engineer is that what you want?
https://code.sololearn.com/W8i5FpPHY1J5/?ref=app
+ 2
You may find answer in :-
1) https://www.geeksforgeeks.org/javascript-math-random-function/
2) https://www.w3schools.com/js/js_random.asp
+ 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
+ 2
㋛︎ᴥ︎𓃰︎
+ 2
Lloyd L Conley is right.✅✔️
+ 1
Thank you, sounds great!
+ 1
It generates a random background color
+ 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?
+ 1
Kelvin Paul I'm trying to create a random animation event.
+ 1
Unrealistic Engineer You mean when click the box it might go to any of directions?
+ 1
YCS-Venom Yes. :)
+ 1
And the movement is const or random too? Unrealistic Engineer
+ 1
YCS-Venom Movement can be const. Three options: Left, up and down.
+ 1
Thank you! Yes, it's nearly perfect. The only thing missing is continuous movement when the box moves to different directions.
+ 1
Unrealistic Engineer just a minute I will do it