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 8037837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:02:40+00:00 2026-06-05T03:02:40+00:00

I have a function ‘generateRan’ that generates random numbers. This function can not be

  • 0

I have a function ‘generateRan’ that generates random numbers. This function can not be changed.

int generateRan() {
    Random num = new Random();
    return (1 + num.nextInt(100));
}

I have to write code that will:

  1. Print numbers 1-20 randomly.
  2. Print numbers 1-200 randomly.

Each number should be printed only once.

The function can be used any number of times. But it is a bit heavy so I want to make the code more optimized.

Here is what I’ve coded:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Test {

    public static void main(String[] args) {
        List<String> list = new ArrayList();
        Test t = new Test();
        iniList(list, 20);

        for (Integer i = ((t.generateRan()) % 20); list.size() > 0; i = 1+((t
                .generateRan()) % 20)) {
            if (list.contains(i.toString())) {
                list.remove(i.toString());
                System.out.println(i);
            }
        }

        System.out.println("********");

        iniList(list, 200);


        for (Integer i = ((t.generateRan()%2)*100 + t.generateRan()) ; list.size() > 0; i = ((t.generateRan()%2)*100 + t.generateRan())) {
            if (list.contains(i.toString())) {
                list.remove(i.toString());
                System.out.println(i);
            }
        }

    }

    private static void iniList(List list, int i) {
        for (Integer k = 1; k <= i; k++) {
            list.add(k.toString());
        }
    }

    int generateRan() {
        Random num = new Random();
        return (1 + num.nextInt(100));
    }

}

Currently the code for 1-200 is incorrect.

  • 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-05T03:02:42+00:00Added an answer on June 5, 2026 at 3:02 am

    Each numbers should print only once

    Then all you need to to is create a List<Integer> of the entire range, then call Collections.shuffle.

    private static void displayNumbers(int minInclusive, int maxInclusive) {
        List<Integer> list = new ArrayList<Integer>();
        for (int i = minInclusive; i <= maxInclusive; i++) {
            list.add(i);
        }
        Collections.shuffle(list);
        for (int value : list) {
            System.out.println(value);
        }
    }
    

    Personally I’d normally use parameters of minInclusive, maxExclusive or minInclusive, count, but it looks like it may be more readable this way for your situation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have function that receives an array of pointers like so: void foo(int *ptrs[],
I have a strange problem. If have function that does this return the value,
I have function like this typedef vector<vector<string> > vecArray; vecArray dul(); vecArray dul() {
I have function getMemory() which returns VARIANT (mfc). It is said that in ulVal
I have function that has : ob_start(); //Include of some files $content = ob_get_contents();
I have function as below: foo :: Int -> a -> [a] foo n
i have function public Menu Details(int? id) { return _dataContext.Menu.Include(ChildMenu).FirstOrDefault(m => m.MenuId == id);
I have function getCartItems in cart.js and I want to call that function in
I have function Start() that is fired on ready. When I click on .ExampleClick
I have function that have been colled in stored procedure. The function returns a

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.