Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Random Number Generator for HTML?
I am looking for a HTML code that can generate a single random number between (and including) 1 and 38 and assign it to a variable. (I'm calling that variable "ran")
I would really appreciate any help.
I was talking about generating random numbers within a SCRIPT tag. I'm trying to make the webpage play a random song and I have everything working properly except the random number generator. The code I'm using does seem to have access to all the possible choices.
When I inserted J P's code,
var randNum = Math.ceil(Math.random()*10);
it seems to work. I have one question. Does the number range include the endpoints?
4 Answers
- J PLv 41 decade agoFavorite Answer
HTML is really not capable of generating random numbers. You can use a simple bit of Javascript to generate one, however. You can use:
<script type="text/javascript">
var randNum = Math.ceil(Math.random()*10);
</script>
to generate a random number between 1 and 10, if you need a different range, you can change the 10 above to whatever you want the upper bound to be.
- TinaLv 45 years ago
I want to create two three digit number generators where the value of both generators will never be same in html....
- Anonymous1 decade ago
HTML is a content descriptor, not a programming language. You'll have to use javascript (client side) or something like php (server side) to do that. You also can't "assign variables" in html - strictly speaking, there are none.