Question:
how can i generate a random number in javascript? ?
Alex s
2008-11-12 09:48:35 UTC
hi yeah i'm some trouble wit this javascript program. im suppose to use innerhtml to display a random number from 1 to 1000. how can i o this and have a button that change the number everytime its clicked?are there any websites , any help is appreciated.
Three answers:
richarduie
2008-11-12 10:47:49 UTC













onclick="showRandom( 1000 )" />







anonymous
2016-11-04 05:35:51 UTC
Math.random() already provides a pseudorandom selection from 0 to one million. To get a random floating factor selection interior of a definite selection, first define the selection by utilising 'min' and 'max', then try this: min + (Math.random() * (max - min)) notice whether that this might in no way generate the optimum value. it would desire to come rather close, however the optimum value is genuinely no longer generated. in case you elect it to be possible to generate the optimum value, or you elect greater randomness than the equipped-in pseudorandom generator grants, there are uncomplicated procedures to construct greater stepped forward structures that cut back weaknesses like that. whether, in addition they'll take longer to run.
anonymous
2008-11-12 09:53:53 UTC
Math.random()



Will return a random number between 0 and 1



You multiply it by the largest number you want and you can round it to make it an integer.



so



function rnd(max)

{

return Math.Round(Math.Random() * max);

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...