In: Computer Science
I am practicing questions to studying with and the prompt is to:
Write statements that assign random integers to the variable n in the following ranges:
This is what I have thus far but when it displays my fourth value that is supposed to be in between 1000 and 1114 is does not confine between those values, am I doing something wrong/is there a way to simplify my work? Looking for a detailed answer with an explanation to really understand this. This is what I have:
<html>
<head>
<meta charset = "utf-8">
<title>Random integers to variable</title>
<style type = "text/css">
p { margin: 0px; }
p.space { margin-top: 10px; }
</style>
<script>
var value1;
for ( var n = 1; n <= 1; ++n )
{
value1 = Math.floor( 1 + Math.random() * 7);
document.writeln( "<p>" + value1 + "</p>" );
}
var value2;
for ( var n = 1; n <= 1; ++n )
{
value2 = Math.floor( 1 + Math.random() * 221);
document.writeln( "<p>" + value2 + "</p>" );
}
var value3;
for ( var n = 1; n <= 1; ++n )
{
value3 = Math.floor( 0 + Math.random() * 49);
document.writeln( "<p>" + value3 + "</p>" );
}
var value4;
for ( var n = 1; n <= 1; ++n )
{
value4 = Math.floor( 1000 + Math.random() * 1114);
document.writeln( "<p>" + value4 + "</p>" );
}
var value5;
for ( var n = 1; n <= 1; ++n )
{
value5 = Math.floor( -2 + Math.random() * 9);
document.writeln( "<p>" + value5 + "</p>" );
}
var value6;
for ( var n = 1; n <= 1; ++n )
{
value6 = Math.floor( -13 + Math.random() * 21);
document.writeln( "<p>" + value6 + "</p>" );
}
</script>
</head>
<body></body>
</html>
Math.floor( 1000 + Math.random() * 1114);
In your random function Math.random() * 1114 will generate random number between 0 to 1114. and then add 1000 to it.
You can see the behaviour of Math.random()*114 in below image.
Suppose random number gerated by Math.random() * 1114 is 1036. and then it will add 1000 to it. So number becomes 1000+1036 = 2016 .
To generate random number between specified range you have to use below formula.
value = Math.floor(Math.random() * (max - min + 1)) + min
1000 ≤ n ≤ 1114 for this case maximum is 1114 and minimum is 1000.
total number is this range are 115.
value4 = Math.floor(Math.random() * (1114 - 1000 + 1)) + 1000
so value4 = Math.floor(Math.random()*115)+1000
Math.random()*115 will generate random number between 0 to 115, then add 1000 to it so it will give random number between range 1000 to 1114.
I have modified the code. you can verify this.
<html>
<head>
<meta charset = "utf-8">
<title>Random integers to variable</title>
<style type = "text/css">
p { margin: 0px; }
p.space { margin-top: 10px; }
</style>
<script>
var value1;
for ( var n = 1; n <= 1; ++n )
{
value1 = Math.floor(Math.random() * 7) + 1;
document.writeln( "<p>" + value1 + "</p>" );
}
var value2;
for ( var n = 1; n <= 1; ++n )
{
value2 = Math.floor(Math.random() * 221) +1;
document.writeln( "<p>" + value2 + "</p>" );
}
var value3;
for ( var n = 1; n <= 1; ++n )
{
value3 = Math.floor(Math.random() * 50) + 0;
document.writeln( "<p>" + value3 + "</p>" );
}
var value4;
for ( var n = 1; n <= 1; ++n )
{
value4 = Math.floor(Math.random() * 115) + 1000;
document.writeln( "<p>" + value4 + "</p>" );
}
var value5;
for ( var n = 1; n <= 1; ++n )
{
value5 = Math.floor(Math.random() * 12) - 2;
document.writeln( "<p>" + value5 + "</p>" );
}
var value6;
for ( var n = 1; n <= 1; ++n )
{
value6 = Math.floor(Math.random() * 35) - 13;
document.writeln( "<p>" + value6 + "</p>" );
}
</script>
</head>
<body></body>
</html>