最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascripthtml: How to generate random number between number A and number B? - Stack Overflow

programmeradmin2浏览0评论

We have a form with 2 fields and a button. We want on button click to output random int number (like 3, 5 or 33) which would lie between int A and int B? (no use of jQuery or anything like it is required)

We have a form with 2 fields and a button. We want on button click to output random int number (like 3, 5 or 33) which would lie between int A and int B? (no use of jQuery or anything like it is required)

Share Improve this question edited Dec 30, 2010 at 3:42 user372551 asked Dec 29, 2010 at 23:37 RellaRella 66.9k112 gold badges373 silver badges642 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 13

You can use Javascript Math.random

function randomInRange(start,end){
       return Math.floor(Math.random() * (end - start + 1) + start);
}

Use something like Math.floor(Math.random()*(intB-intA +1)) + intA ?

Like this:

Math.floor(a + Math.random() * (b - a))

The Math.random() method returns a random floating-point number in the range [0,1) — that is, between 0 (inclusive) and 1 (exclusive).

发布评论

评论列表(0)

  1. 暂无评论