Question

In: Computer Science

I am practicing questions to studying with and the prompt is to: Write statements that assign...

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:

  1. a) 1 ≤ n ≤ 7
  2. b) 1 ≤ n ≤ 221
  3. c) 0 ≤ n ≤ 49
  4. d) 1000 ≤ n ≤ 1114
  5. e) –2 ≤ n ≤ 9
  6. f) –13 ≤ n ≤ 21

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>

Solutions

Expert Solution

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>


Related Solutions

Hello, I am studying and I am a bit confused about registers. There are segments registers,...
Hello, I am studying and I am a bit confused about registers. There are segments registers, data registers, pointer registers, index registers. But I do not really understand where these are found. Are they found in 8086 architecture? For instance if I ask what type of registers are found in 8086 architecture what will be the answer? All of these or only segment registers?
I am struggling with figureing this out as a practicing question! Please show me the work...
I am struggling with figureing this out as a practicing question! Please show me the work for how to get the correct answers! ABC Company produces two separate products, product A and product B.  Information on product A and B is reported below: Product A                                Product B Sales Price:                  $1,000                                     $1,800 Direct Materials          $100                                        $500 Direct Labor                $250                                        $600 Variable Overhead      $50                                          $400 Commissions:             $25                                          $40 Company information: Fixed Manufacturing overhead:          $400,000 per year Fixed Administrative Expenses:          $375,000 per year Tax rate:                                              25% ABC Company has to decide which of the following sales mixes will...
In the given instruction, I am to use while loops only. The goal is to prompt...
In the given instruction, I am to use while loops only. The goal is to prompt the user to select an acceptable input. If the wrong input is given, the program forces the user to select an appropriate input. The program also keeps running until the user chooses to exist by selecting a very specific input which in my case is the upper or lower case "E". The problem is even after selecting upper or lower case "E", my program...
My name is Donna. I am a college student studying to be an English teacher. I...
My name is Donna. I am a college student studying to be an English teacher. I dislike inflation, my degree is costing me more than i had anticipated. Good thing that I am a senior, tuition when I started three years ago was 20% less. Maybe I should get my masters but that would put me unto more debt. Why do we have so much inflation? I work part time as a server and have a low fixed rate loan...
Getting an error with my for loop.  How do I correct it?  I am supposed to: Continuously prompt...
Getting an error with my for loop.  How do I correct it?  I am supposed to: Continuously prompt for the number of minutes of each Rental until the value falls between 60 and 7,200 inclusive. For one of the Rental objects, create a loop that displays Coupon good for 10percent off next rental as many times as there are full hours in the Rental. ///////////////////////////////RentalDemo package java1; import java.util.Scanner; public class RentalDemo { public static void main(String[] args) {    Rental object1 =...
I am studying for Investments 10th by Bodie Kane in my University. As doing assignments, I...
I am studying for Investments 10th by Bodie Kane in my University. As doing assignments, I have these questions concerning margin problems. Could any experts help me to solve these questions?:) Rachel opens a margin account and purchases 400 shares of Ross Inc. at $40 per share. The initial margin requirement is 50%. The share price falls to $25 per share by the end of the year. 1) What is the remaining margin in the account? 2) If the maintenance...
Not sure how to prompt these functions here. I am trying tocreate a holiday shopping...
Not sure how to prompt these functions here. I am trying to create a holiday shopping list. Here are my three requirements'''This function should prompt the user for the names of all the family members that they will be purchasing gifts for this holiday and return those names in a list'''def family_names():names = []return names'''This function takes the names of all family members as a list and asks the user how much money they will spend on each person. These...
Prompt: I am tired of this nation worrying about whether we are offending someone or their...
Prompt: I am tired of this nation worrying about whether we are offending someone or their culture. I am not against immigration, nor do I hold a grudge against anyone who is seeking a better life by coming to America. However, there are a few things that those who have recently come to our country, and apparently some born here, need to understand. This idea of America being a multicultural community has served only to dilute our sovereignty and our...
I am completing a questionnaire for my course. How can I re-write following questions as closed-ended...
I am completing a questionnaire for my course. How can I re-write following questions as closed-ended questions to be included in the questionnaire? 1.Do you believe that World Bank loans significantly helped economic development in Emerging Markets during 2008-2016. 2. Please indicate the extent to which you agree with the following statements: a) The World Bank helped improve the enabling environment for SME firms in Emerging Markets during 2008-2016. b) The World Bank’s mainstreaming of framework agreements addressed project implementation...
i am currently studying long-term liabilities and am confused between the equity financing and bond financing....
i am currently studying long-term liabilities and am confused between the equity financing and bond financing. what are the pros and cons of each financing?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT