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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:29:15+00:00 2026-05-20T14:29:15+00:00

Sounds simple enough…but I’ve been plugging away at this, trying to find the one

  • 0

Sounds simple enough…but I’ve been plugging away at this, trying to find the one and all solution.

For a range of numbers, say 1-12, I want to generate a random sequence within that range, and include 1 and 12.

I don’t want duplicate numbers though.

So I would want something like this – 3,1,8,6,5,4 ..and so on, every number from 1-12.

Then I want to put these random numbers into an Array and use that array to ‘randomly’ select and display some items (like inventory pulled from database) on a jsp page.

The problem with what I’ve tried thus far, is that there are a lot of duplicate numbers being generated…or, not ALL of the numbers are chosen.

Is there a simple solution to this problem?


Edit

Test#1 using Collections and shuffle() method –

ArrayList<Integer> list = new ArrayList<Integer>(10);
for(int i = 0; i < 10; i++)
{
  list.add(i);
}
Collections.shuffle(list);

String[] randomNumbers = (String[])list.toArray();

for(int i = 0; i < 10; i++)
{
  out.print(randomNumbers[i]+"<br>");
}

The result was a sequence with duplicate values –
chose = 3
chose = 8
chose = 7
chose = 5
chose = 1
chose = 4
chose = 6
chose = 4
chose = 7
chose = 12

Test #2 – using Random math class

int max = 12;
int min = 1;

int randomNumber = 0;

String str_randomNumber = "";

for(int i=0; i<10; i++) {
    //int choice = 1 + Math.abs(rand.nextInt(11));
    int choice = min + (int)(Math.random() * ((max - min) + 1));

    out.print("chose = "+choice+"<br>");
}

The result was just like using Collections.shuffle().

  • 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-05-20T14:29:16+00:00Added an answer on May 20, 2026 at 2:29 pm

    You can fill an array with all values from 1 to 12 and then shuffle them (see e.g. Why does Collections.shuffle() fail for my array?)

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

Sidebar

Related Questions

This sounds like a simple enough question, but can't find the answer for the
This sounds simple enough but I haven't been able to figure out how to
This sounds simple enough, but so far I have been unable to solve it.
Sounds simple enough, but its not working. In this example, I want to set
I'm trying to set the text of a TextView which sounds simple enough but
This sounds simple but my regex knowledge is limited. I need an expression to
This sounds like a really simple question, but I am new to PHP. If
Sounds simple enough, but I'm feeling quite stupid today. If I have an array
Unfortunately, the answer to this question isn't quite as simple as it sounds... but
I am trying to rename a button from a string. Sounds simple enough? Well

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.