Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You must add code (to what has been provided below), so that your finished progr

ID: 3830586 • Letter: Y

Question

You must add code (to what has been provided below), so that your finished program accomplishes 3 things: 1. Randomly generates, and then prints to the Console, an integer between 10 and 30 (including both 10 and 30), by whatever means you want to. Randomly generates, and then prints to the Console, any number between -4 and 4 (including both -4 and 4) by using only the .Next() method that takes no parameter. Either print the word Heads or the word "Tails" on the screen. Your code must randomly choose which message to print on the screen. You should write your code in a way that emphasizes correctness and efficiency, much like you've had to emphasize in your code for homework assignment/PCEs using System;//this program is legal, but oddly indented {class Program {static void Main(string [] args) {

Explanation / Answer

Source Code:

using System.IO;
class Program
{
static void Main(string[] args)
{
int num1, num2, num3;
  
Random r = new Random();
  
num1 = r.Next(10,31);
Console.WriteLine("Randomly generated number between 10 and 30 (both inclusive) is: "+ num1);
  
num2 = r.Next();
  
if(!(num2>=-4 && num2<=4))
{
while(!(num2>=-4 && num2<=4))
{
num2 = r.Next();
}
}
  
Console.WriteLine("Randomly generated number between -4 and 4 (both inclusive) is: " + num2);
  
num3 = r.Next();
  
if(num3%2==0)
{
Console.WriteLine("Heads");
}
  
else
{
Console.WriteLine("Tails");
}
}
}

Output:

Output 1:

Randomly generated number between 10 and 30 (both inclusive) is: 18

Randomly generated number between -4 and 4 (both inclusive) is: 2
Heads

Output 2:

Randomly generated number between 10 and 30 (both inclusive) is: 17
Randomly generated number between -4 and 4 (both inclusive) is: 4
Heads

Output 3:

Randomly generated number between 10 and 30 (both inclusive) is: 12
Randomly generated number between -4 and 4 (both inclusive) is: 1
Tails

Description:

So this is the code you are required here.

In this code, I am using 3 integers for 3 operations, 1 for each individual task.

To generate the random number, the class is there : class Random which gives the pseudo random number. It is part of System name space so you can use it here as you imported it in the first line of the code.

So, I am creating a variable 'r' of Random class which has a Next() method which takes 0,1 or 2 arguments and generates the number as per it.

For example, in (1) you need to create a random number between 10 and 30 both inclusive. So you will pass 2 arguments here. The behavior of this method is such that the first value is taken as min value, second value is the max value and it generates the number between min and max, min inclusive and max exclusive.

In short if you write r.Next(10, 30), it will return a number from 10 to 29, 30 will never returned from this call. As you are told to include 30 also, you will write it as r.Next(10, 31) so that now it will generate from 10 to 30 both inclusive.

So I am generating a number randomly in this way and then assigning it to num1 and printing it to the console.

The next number should be between -4 and 4 but you are supposed not to use any parameters inside Next() method so there's also a way for the same.

You will generate a number without specifying limit, then check whether it falls within the range or not. If it comes in the limit then it is fine otherwise until the condition is true, continuously generate the number and check continuously till the condition is met.

So as we don't know the number of iterations, while loop is the best fit here. So I have used it and then assigning the value to num2 and printing it on the console.

It is possible that the second line printing will take some more time, until while loop finds a number between this range, it will continue execution so it may take time. If the number is found in the first call of num2 = r.Next(), the while loop won't get executed. In this case the output will come earlier.

For the third part, I am using a logic that I am again creating a random number, without assigning a limit, and storing it in num3.

If num3 is even, its remainder when dividing by 2 will be 0. So, if the remainder is 0, i.e. if even number is generated, I am printing "Heads" otherwise "Tails" gets printed.

You can apply any logic here : like for positive values, print "Heads" otherwise "Tails" or for 0 only print "Heads" otherwise "Tails" or any logic you can apply here. The main thing is the output is determined randomly based on num3 value.

So, when I executed the same code, I got the above outputs and as it was an online IDE, I have copied its output.

Check the output and the description. And do comment for further queries if any.. I will be happy to address them.

Thank you. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote