Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8030827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:53:36+00:00 2026-06-05T00:53:36+00:00

Write an efficient algorithm to print the following two outputs You are given a

  • 0
Write an efficient algorithm to print the following two outputs

You are given a pre-defined function named getrand100() which returns
an integer which is one random number from 1-100. You can call this
function as many times as you want but beware that this function is
quite resource intensive. You cannot use any other random generator.
You canNOT change the definition of getrand100().

int getrand100(){
    Random rand = new Random();
return (1+rand.nextInt(100));      
}
  • Output1: Print numbers 1-20 in random order. (Not 20 random numbers)
  • Output2: Print numbers 1-200 in random order. (Not 200 random numbers)

Note:

  • i. Every number should be printed exactly once.
  • ii. There should be no pattern in the numbers listing. List should be completely random
    i.e., all numbers have equal probability appearing at any place.
  • iii. You may call getrand100() any number of time to get randomnumber
    from 1 to 100.
  • iv. You cannot use any other random generator
    function except getrand100().
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:53:37+00:00Added an answer on June 5, 2026 at 12:53 am

    The idea is to use the Random generator given to compute the required random numbers.

    1) For random numbers 1-20, just divide the 100 numbers equally to represent 1 to 20.

    2) To generate 1-200, find the even numbers from 1 to 200 and then add (-1 or 0) to it to get all the numbers from 1 to 200.

    import java.util.*;
    public class Rand20_200{
       int number20[]=new int[20]; //numbers in random order
       int number200[]=new int[200];
    
       public Rand20_200(){
        int n=0;
        int ngen[]=new int[20]; //to store which random numbers are generated
        while(n<20){
          int rnd=1 + (getrand100()-1) / 5;
          if (ngen[rnd-1]==0){
            ngen[rnd-1]=1;
            number20[n++]=rnd;
          }
        }
        System.out.println("Random 20 numbers");
        print(number20);
    
        ngen=new int[200]; //to store which random numbers are generated
        int numoff[]={-1,0}; //offset to add
        n=0;
        while(n<200){
          int rnd=numoff[(getrand100()-1)/50]+ (getrand100()*2);
          if (ngen[rnd-1]==0){
         ngen[rnd-1]=1;
         number200[n++]=rnd;
          }
        }   
        System.out.println("\nRandom 200 numbers");
        print(number200);
       }
    
       int getrand100(){
        Random rand = new Random();
        return (1+rand.nextInt(100));      
       }
    
       void print(int arr[]){
         for(int i=0;i<arr.length;i++){
           System.out.print(arr[i]+" ");
         }
       }
    
       public static void main(String args[]){
         new Rand20_200();
       }
    
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write an efficient algorithm that will effectively let me merge
I'm wondering what the best and most efficient way to write up the following
I need to write an efficient algorithm for looking up words with missing letters
How can I write an algorithm that given a floating point number, and attempts
I am looking for help to write an efficient PHP algorithm to help me
Problem: Given a matrix in which each row and each column is sorted, write
I want to write a function that returns the nearest next power of 2
What's the most efficient algorithm to find the rectangle with the largest area which
How would I write an efficient algorithm to search for a subset array of
I'm trying to write more efficient code in a C program, and I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.