Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now 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
Javascript to create a random number?
I do not know Javascript at all. I do know basic HTML for displaying text, images, and tables. I need to display a random number between 1 and 10 on a web page. Can someone write some Javascript to do this? Bonus if you can write code to create 6 unique random numbers to be displayed on an .html page.
5 Answers
- ArmchairPilotLv 61 decade agoFavorite Answer
Here's an algorithm that shuffles the numbers by switching numbers in random positions a random number of times:
<html>
<head>
<title>Shuffle</title>
<script>
var n = [];
function deal() {
for (var i=1; i<=10; i++) { n[i] = i;}
var nShuffles = Math.ceil(Math.random()*20)+10;
for (i=1; i<=nShuffles; i++) {
var p = 1;
var q = 1;
// get two different random numbers p and q between 1 and 10
while (p == q) {
p = Math.ceil(Math.random()*10);
q = Math.ceil(Math.random()*10);
}
// switch numbers
var x = n[p];
n[p] = n[q];
n[q] = x;
}
// display
strD = n[1];
for (i=2; i<=10; i++) { strD += ", " + n[i]; }
document.getElementById("seq").innerHTML = strD;
}
</script>
</head>
<body onload="deal();">
<b>
<p id="seq"></p>
<input type="button" value=" Press " onclick="deal();">
</body>
</html>
Just copy and paste the code into a text editor, save and open in your browser. I've included a button to re-shuffle
- deckelmanLv 45 years ago
In JavaScript, the duty operator, a unmarried = (equals sign), return the cost that became assigned to it. this option enables you to do numerous variable assignments so which you're able to do issues like this: var num1 = num2 = num3 = 10; ... and all 3 variables would be assigned the form ten. by using some it particularly is seen undesirable form, yet you are able to the two assign and study values interior the situation fact of a whilst loop (it particularly is definitely a repeated "if" fact.) you do no longer inevitably ought to positioned something interior the loop itself. for occasion: <script variety="text cloth/javascript"> var numList = []; whilst (0.8 > (numList[ numList.length ] = Math.random())) {}; alert("form(s): " + numList.connect(", ")); </script> in spite of the shown fact that the above syntax ought to bring about sloppy coding practices, it is important understand that it particularly is finished in JavaScript.
- 1 decade ago
Short and sweet version:
<script language="javascript" type="text/javascript">
for (i=1;i<=6;i++)
{
var randomNumber = Math.ceil(Math.random() * 10);
document.write(randomNumber + '<br />');
}
</script>
- Anonymous1 decade ago
Here
<html>
<head>
<script language="JavaScript" type="text/javascript">
/***to generate random numbers, call math.random().
to display them, use document.write or document.writeline
function***/
var randomNumber;
for(var i = 0; i < 6; i++)
{
randomNumber = Math.random(); /**returns a random number less than 1**/
randomNumber*=10; /***make it a number from one and 10**/
/**use the Math.ceil method to round it to the nearest number**/
document.write(Math.ceil(randomNumber) + '<br>');
}
</script>
</head>
<body>
</body>
</html>
- 1 decade ago
Javascript can be modified, creating random numbers can be changed by users.
For this, i would suggest php.
Good luck.