Home >Web Front-end >JS Tutorial >Javascript generates random numbers in a specified range_javascript skills

Javascript generates random numbers in a specified range_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:56:30900browse

But after a little hard work, I finally figured it out, and the problem was naturally solved.
Then I wrote a formula, which should make this usage disappear. Formula:
1 . Starting from 1 to any value
linenum
parseInt(Math.random()*upper limit 1);
2. Starting from any value to any value
linenum
parseInt(Math.random ()*(upper limit - lower limit 1) lower limit);
The above formula uses parseInt(), so you need to add 1; if you use Math.ceil(), you don’t need to add 1. I am used to writing like this...
Directory:
1. Demonstration 1 (directly perform the operation of generating random numbers)
2. Demonstration 2 (write as a function to perform the operation of generating random numbers)
1. Demonstration 1 (directly perform the operation of generating random numbers) )
linenum

1-10:

11-20:

1-100:

51-100:


2. Demonstration 2 (written as a function to generate random numbers)
linenum

1-10:

11-20:

1-100:

51-100:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn