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

  • Home
  • SEARCH
  • 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 8975959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:00:37+00:00 2026-06-15T19:00:37+00:00

In my Android app, I need to pick two random integers from a specified

  • 0

In my Android app, I need to pick two random integers from a specified range. The below code works, but it’s not really picking a random integer, it’s shuffling the ints. And the thing that bugs me is that I have to specify the max int as one less than is actually the max value because of the randomNum2= (int)it.next(); bit in the code. If I put the correct number (currently 127) as the max, then I get the error java.util.NoSuchElementException because it’s looking for 128 as the next iteration. Before I was using rand = new Random();
randomNum1 = rand.nextInt(max - min + 1) + min;
randomNum2 = rand.nextInt(max - min + 1) + min;
But the problem there is the two random ints may end up being the same, and I need them to be unique. So can anyone suggest a better method for getting TWO random numbers from a min/max integer range? Here’s my code:

    int min = 1;
int max = 126;
int randomNum1;
int randomNum2;
List<Integer> choicesL;

public void randomTwo() {
    choicesL = new LinkedList<Integer>();
    for (int i = min; i <= max; i++) {
        choicesL.add(i);
    }
    Collections.shuffle(choicesL);
    Iterator<Integer> it = choicesL.iterator();
    while (it.hasNext())
    {
        randomNum1= (int)it.next();
        randomNum2= (int)it.next();

    }

}
  • 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-15T19:00:38+00:00Added an answer on June 15, 2026 at 7:00 pm

    You can use your old code but with a loop while.

    rand = new Random(); 
    randomNum1 = rand.nextInt(max - min + 1) + min;
    randomNum2 = rand.nextInt(max - min + 1) + min;
    
    while (randomNum2==randomNum1){
        randomNum2 = rand.nextInt(max - min + 1) + min;
    }
    

    EDIT

    There are many ways to look for a random number. I don’t know if you know this but below this there are lots of statistical studies. If you need a fast way to calculate random numbers this is great. But you have to know that after a big time an user can know how is the distribution of your random numbers. If you want to improve your code maybe you can check:

    http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform

    http://en.wikipedia.org/wiki/Ziggurat_algorithm

    And of course I supose that there are much more theory on the net

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

Sidebar

Related Questions

I'm writing an android app and I need to read several files from several
I'm building an Android app and I need to have two different themes for
In my android app I need to display in a gridview images from a
In my android app I need a layout as shown below A relative layout
In my android app I need to get the country code of the current
In my Android app I need to extract data from a xml file (the
In my Android app I need dynamically create few LinearLayout's with text. But I
My android app project need to add a new function of saving the click
Android Calendar App I need to be able to implement a view similar to
To create facebook android native app we need to provide our Android application signature

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.