Question

Javascript Write a program to generate random numbers between a user-specified range, then count and display...

Javascript

Write a program to generate random numbers between a user-specified range, then count and display the frequencies of the most / least appeared numbers.
Sample Output
How many random numbers? 1000000
Enter the minimum number: 100
Enter the maximum number: 200
Generated 1000000 random numbers between 100 (inclusive) and 200 (exclusive).
Number 133 has the most occurrences (10197).
Number 154 has the least occurrences (9749).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

HTML CODE:

<!DOCTYPE html>
<html>
<head>
   <title>Random Numbers</title>
</head>
<body>
   <h1 id="text">Genarating Random Numbers</h1>
   How many random numbers? <input type="text" id="total" ><br><br>
   Enter the minimum number: <input type="text" id="min" ><br><br>
   Enter the maximum number: <input type="text" id="max" ><br><br>
   <button id="cal">Count</button><br><br>
   <p id="m1"></p>
   <p id="m2"></p>
   <p id="m3"></p>
   <script type="text/javascript" src="ak.js"></script>
</body>
</html>

JAVA SCRIPT CODE:

function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
//GENARATING RANDOM NUMBER INBETWEEN INCLUSIVE RANGE
return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive
}

document.getElementById('cal').addEventListener('click',function(){
   var total=document.getElementById('total').value;
   var min=document.getElementById('min').value;
   var max=document.getElementById('max').value;
   //CONVERT TEXT VALUES INTO INTEGER VALUES
   total=parseInt(total, 10);
   min=parseInt(min, 10);
   max=parseInt(max, 10);
   var arr=[];
   //STORE EACH GENARATED VALUE TO THE ARRAY
   for(var i=0;i<total;i++){
       arr.push(getRandomIntInclusive(min, max));
   }
   var counts = {};
   //FIND THE OCCURENCES OF EACH NUMBER
   for (var i = 0; i < arr.length; i++) {
       var num = arr[i];
       counts[num] = counts[num] ? counts[num] + 1 : 1;
   }
   var m=arr.length;
   var M=0;
   var mi,ma;
   //FIND THE MOST AND LEAST OCCURENCE NUMBERS
   for (var val of Object.keys(counts)){
       if(counts[val]>M){
           M=counts[val];
           ma=val;
       }
       if(counts[val]<m){
           m=counts[val];
           mi=val;
       }
   }
   document.getElementById('m1').innerHTML="Generated "+total+" random numbers between "+min+" (inclusive) and "+max+" (inclusive)."
   document.getElementById('m2').innerHTML="Number "+ma+" has the most occurences ("+M+").";
   document.getElementById('m3').innerHTML="Number "+mi+" has the least occurences ("+m+").";
});

OUTPUTS:

AFTER CLICK COUNT

Add a comment
Know the answer?
Add Answer to:
Javascript Write a program to generate random numbers between a user-specified range, then count and display...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For this assignment, write a program that will generate random numbers in the range of 50-100...

    For this assignment, write a program that will generate random numbers in the range of 50-100 and count how many fall into particular ranges. Processing: The program should first seed the random number generator. This is done one time in the program. Use srand(0). Next, generate and display a series of 10 random numbers between 50 and 100. There are several ways to ensure that a random number falls between 50 and 100. One possibility is to use the following...

  • Only part 1, makinf the 2nd and 3rd picture of code do the same thing using...

    Only part 1, makinf the 2nd and 3rd picture of code do the same thing using array Purpose This homework is to learn the concept and usage of one-dimensional (ID) arrays. Part 1 (30 pts) Modify your HW6' (i.e., digit frequency histogram) in a new program (DigitFrequencyArray.java) to use a count array instead of 10 count variables. Your new program should be much shorter than your original code since there is no need for branching statements (if or switch). For...

  • Write a program that writes a series of random numbers to a file. Each random number...

    Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500 inclusive. 1.Use a file called randoms.txt as a input file and output file a. If you go to open the file and its not there then ask the user to create the file     and then quit 2. Ask the user to specify how many random numbers the file will hold. a.Make sure that the...

  • Python Code Write a program using functions and mainline logic which prompts the user to enter...

    Python Code Write a program using functions and mainline logic which prompts the user to enter a number. The number must be at least 5 and at most 20. (In other words, between 5 and 20, inclusive.) The program then generates that number of random integers and stores them in a list. The random integers should range from 0 to 100. (You can use a wider range if you want, but the lower end of the range must be at...

  • Write a program to play "guess my number". The program should generate a random number between...

    Write a program to play "guess my number". The program should generate a random number between 0 and 100 and then prompt the user to guess the number. If the user guesses incorrectly the program gives the user a hint using the words 'higher' or 'lower' (as shown in the sample output). It prints a message 'Yes - it is' if the guessed number is same as the random number generated. ANSWER IN C LANGUAGE. ====================== Sample Input / Output:...

  • I need to create a bit of code for javascript which I can have random numbers...

    I need to create a bit of code for javascript which I can have random numbers generate with a certain amount of digits set by the user. and ask the user how many times they want this to happen. For example The user says they want a number with 4 digits (any number between 0 - 9999) and the user can input if they want to add, subtract, multiply, divide, or make it random 2 random numbers are generated. First...

  • Write a program that prompts the user to enter a number within the range of 1...

    Write a program that prompts the user to enter a number within the range of 1 to 10 inclusive. The program then generates a random number using the random class: If the users guess is out of range use a validation loop (DO) to repeat the prompt for a valid number, If the users guess matches the random number tell them they win! If the users guess does not match the random number tell them they are wrong. Use a...

  • Write the Code in C program. Create a random password generator that contains a user-specified number...

    Write the Code in C program. Create a random password generator that contains a user-specified number of characters. Your program should prompt the user for the desired length of the password. Length of password: To create your password, use the random number generator in the stdlib.h C library. To generate random numbers, you will need the srand() and rand() functions. srand(seed) is used to "seed" the random number generator with the given integer, seed. Prompt the user for the seed...

  • in c Q3) Write a program to generate three random numbers between 1 and 9 and...

    in c Q3) Write a program to generate three random numbers between 1 and 9 and display them. The user has to enter their sum as soon as possible. These two steps will be repeated continuously. An alarm will be triggered every 5 seconds to display the numb answers so far. If the user did not give a correct answer in 10 seconds, the timer will jump to the main to give an error message "Too slow response!" and terminate...

  • Write a program which gives an easy mathematics quiz. The program should display two random numbers...

    Write a program which gives an easy mathematics quiz. The program should display two random numbers which are to be added together, like this: 117 + 213 ----- The program should ask the user to enter their answer. If the answer is correct, the user should be congratulated. If the answer is wrong, the right answer should be displayed and the user should be scolded. Don't forget to: Generate random numbers Ask the user if they want to be tested...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT