I don't understand something Why Math.floor(Math.random() * 10) + 1; returns a random integer from 1 to 10? and Math.floor(Math.random() * 11); returns a random integer from 0 to 10 not 11? Math.floor(Math.random() * 10) + 5 gives you a number like 12? Why they are different from each other and they have the same attribute?
Is there a formula for that, I want a full explanation
please?
Math.random() will generate a random number between 0 to 1 so if we multiply with 10 it will random number between 0-9.9999 so if if we do floor() than it will random number between 0-9
in first case Math.floor(Math.random() * 10) + 1
here you will get random numbers from 1-10 because Math.floor(Math.random() * 10) will give you 0-9 for that your adding 1 so it will be 1-10
Math.floor(Math.random() * 11); it will gie 0-10.999 as your using floor it will make 1-10 only and your not adding +1 here if you add you will get 1-11
Math.floor(Math.random() * 10) + 5: here you wll get 5-14
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me
Get Answers For Free
Most questions answered within 1 hours.