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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:55:23+00:00 2026-06-04T21:55:23+00:00

This is my homework assignment: Random r = new Random(); public int get100RandomNumber() {

  • 0

This is my homework assignment:

Random r = new Random();
public int get100RandomNumber() {
    return 1 + r.nextInt(100);
}

You are given a pre-defined function named getrand100() (above) 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().

Output: Print numbers 1-20 in random order. (Not 20 random numbers)

What I have tried..

public class MyClass {

    static Random r = new Random();
    static HashSet<Integer>;

    public static void main(String args[]) {
        myMethod();
        System.out.println(s);
    }    

    public static void myMethod() {
        boolean b = false;
        s = new HashSet<Integer>();
        int i = getRand100();
        if (i >= 20)
            i = i % 20;
        int j = 0;

        int k, l;
        while (s.size() <= 20) 
        {
            System.out.println("occurence no" + ++j);
            System.out.println("occurence value" + i);
            b = s.add(i);
            while (!b) {
                k = ++i;
                if(k<=20)
                    b = s.add(k);
                if(b==true)
                    break;
                if (!b) {
                    l = --i;
                    if(i>=1&&i<=20)
                        b = s.add(l);
                    if(b==true)
                        break;
                }
            }
        }
        System.out.println(s);
    }

    public static int getRand100()
    {
        return r.nextInt(100) + 1;
    }
}

Thanks for any help!

  • 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-04T21:55:24+00:00Added an answer on June 4, 2026 at 9:55 pm

    I believe you are asking how to use a random number generator to print out the numbers 1 to 20 in a random order. This is also known as a “random permutation”. The Fischer-Yates shuffle is such an algorithm.

    However, to implement the algorithm, you first of all need a random number generator that can pick one out of N items with equal probability where N ranges from 2 up to the size of the set to shuffle, while you only have one that can pick one out of 100 items with equal probability. That can easily be obtained by a combination of modulo arithmetic and “rerolling”.

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

Sidebar

Related Questions

Firstly, this is a homework assignment, and I am very new to programming in
This is for a homework assignment to sort some given strings. I'm prompting the
I am working on this homework assignment and I am stuck on what I
This is part of a homework assignment. I've got several questions asking find the
I know this sounds like a homework assignment, but it isn't. Lately I've been
(this is indirectly a part of a much larger homework assignment) I have something
Full disclosure : This is for a homework assignment. This is driving me nuts.
I have a homework assignment to sort an array in ascending order. Obviously, this
This was a homework assignment for my students (I am a teaching assistant) in
This is for a homework assignment, so I don't want the exact code, but

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.