Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript/html: How to generate random number between number A and number B?

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)

like image 421
Rella Avatar asked Dec 29 '10 23:12

Rella


People also ask

How do you get a random number between two numbers in JavaScript?

floor(Math. random() * ((max-min)+1) + min); Generate a random integer between two numbers min and max (the min is inclusive, and the max is exclusive).

How do you generate a random number between 1 and 2 in JavaScript?

Example: Integer Value Between Two Numbers In JavaScript, you can generate a random number with the Math. random() function. The above program will show an integer output between min (inclusive) to max (inclusive). First, the minimum and maximum values are taken as input from the user.

How do you randomize numbers in HTML?

var numRand = Math. floor(Math. random() * 101); That will return a random number between 1-100.


2 Answers

You can use Javascript Math.random

function randomInRange(start,end){
       return Math.floor(Math.random() * (end - start + 1) + start);
}
like image 150
Samet Atdag Avatar answered Sep 28 '22 14:09

Samet Atdag


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

like image 21
Sebastiaan van den Broek Avatar answered Sep 28 '22 15:09

Sebastiaan van den Broek