Question

In: Computer Science

Write a program that uses a custom function to generate a specified number of random integers...

Write a program that uses a custom function to generate a specified number of random integers in a specified range. This custom function should take three arguments; the number of integers to generate, the lower limit for the range, and the upper limit for the range. Values for these arguments should be entered by the user in main. The custom function should display the random integers on one line separated by single spaces. The function should also report how many numbers were even and how many were odd. Finally, the function should calculate the total of the random integers and return this total back to main where it will be printed. User inputs in main are shown in blue.
Sample Output
How many integers are to be generated 8
Enter the lowest integer desired 10
Enter the highest integer desired 20
16 14 19 20 12 15 18 18
6 evens and 2 odds were generated
The total of those 8 random numbers is 132

Solutions

Expert Solution

import java.util.*;
import java.lang.*;
public class Main
{
   public static void main(String[] args) {
       int result = customFunction(8,10,20);

       //result variable will give the total sum

//
      
       System.out.println("The total of those 8 random numbers is "+result);

//above print statement can be changeable based on user requirement
   }
  
   public static int customFunction(int no,int lower,int upper)
   {
   Random rand = new Random();
   int num ,randnum ,even=0,odd=0,total=0;
   //rand is a random object to create random numbers
   for (int i=0 ; i<no ; i++ ){
   //i is a loop iteration variable
   num = rand.nextInt();
   //num will generate random number
   randnum = lower + (int)(Math.random() * ((upper - lower) + 1));

//we have other ways to generate random number in given range.
   // the above step will give the number in between the given range
  
   //if the given number is even it is counted on even variable and same as odd
   if (randnum%2==0)
   even++;
   else
   odd++;
   //in total variable we sum up all the numbers
   total+=randnum;
   //printing the numbers
   System.out.print(randnum+" ");
   }
   System.out.println();
   System.out.println(even+" evens and "+odd+" odds were generated");
   return total;
   }
}


Related Solutions

The language is MATLAB Write a function that will generate three random integers, each in the...
The language is MATLAB Write a function that will generate three random integers, each in the inclusive range from 10 to 80. It will then return a string consisting of the three integers joined together, and also a character vector consisting of the three integers joined together. For example, if the random integers are 11, 29, and 76, the string that is returned will be "112976" and the character vector that is returned will be '112976'. I'm really confused on...
Write a program that does the following: Generate an array of 20 random integers between -100...
Write a program that does the following: Generate an array of 20 random integers between -100 and 100. Compute the average of the elements of the array and find the number of elements which are above the average. For example, if the elements of the array were 5 2 4 1 3 then your program should output The average is 3.0 There are two elements above the average Find the smallest element of the array as well as its index...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1 to 10 then subtract the second integer from the first integer. Note, if the second integer is greater than the first integer, swap the two integers before making the subtraction.Then prompt user for the answer after the subtraction. If the answer is correct, display “Correct”otherwise display “Wrong”.You will need to do this in a loop FIVE times and keep a count of how many...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
1. Write a python program to create a list of integers using random function. Use map...
1. Write a python program to create a list of integers using random function. Use map function to process the list on the expression: 3x2+4x+5 and store the mapped elements in another list. Now use filter to do sum of all the elements in another list. 2. Write a function that takes n as input and creates a list of n lists such that ith list contains first 10 multiples of i. 3. Write a function that takes a number...
write code to count the number of odd integers in an array of 100 random integers...
write code to count the number of odd integers in an array of 100 random integers in the range [0,99].
Write a function that will generate an array of random numbers. It needs to:
DO IN C++Write a function that will generate an array of random numbers. It needs to:Take 3 integers as parameters-The first is the minimum value-the second is the maximum value-the third is the size of the new array-create a dynamically allocated array of the correct size-generate a random number (between min and max) for each element in the array-return a pointer to the arrayCreate a main() function that tests the above function and displays the values in the random array.
Program Name: Divisible. Write a program that calculates the number of integers in a range from...
Program Name: Divisible. Write a program that calculates the number of integers in a range from 1 to some user-specified upper bound that are divisible by 3, the sum of those integers, and the number and sum of those integers not divisible by 3. Your program must display meaningful output which includes: the selected upper bound number of integers divisible by 3 sum of integers divisible by 3 number of integers not divisible by 3 sum of integers not divisible...
Program Name: Divisible. Write a program that calculates the number of integers in a range from...
Program Name: Divisible. Write a program that calculates the number of integers in a range from 1 to some user-specified upper bound that are divisible by 3, the sum of those integers, and the number and sum of those integers not divisible by 3. Your program must display meaningful output which includes: the selected upper bound number of integers divisible by 3 sum of integers divisible by 3 number of integers not divisible by 3 sum of integers not divisible...
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT