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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:52:43+00:00 2026-06-09T06:52:43+00:00

I have looked at Randomize or shuffle an array Randomize or shuffle an array

  • 0

I have looked at Randomize or shuffle an array Randomize or shuffle an array

I am not sure if this is the best approach to make.

I want to randomize the indices of an array with 3 items.

12
4
5

int numbers[] = new int[3];

I tried using the Maths.Random

int randomoption2 = opmin + (int)(Math.random() * ((opmax – opmin) + 1));

but I then have an issue with repetition of the indices values. What is the best approach to randomize the indices so there is no repetition .

eg

a[1] = 2;

I don’t want two elements in the array coming back with an indices of one

http://www.exampledepot.com/egs/java.util/coll_Shuffle.html

public class randomorder {

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

        public static ArrayList randomorder(){
            ArrayList nums = new ArrayList();
            nums.add(1);
            nums.add(2);
            nums.add(3);

            Collections.shuffle(nums);
            return nums;
        }
    }

I now need to store each of the numbers in variables so they can be outputted

System.out.println(options[0]);

  • 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-09T06:52:46+00:00Added an answer on June 9, 2026 at 6:52 am

    Use Collections.shuffle:

    Integer[] numbers = { 1, 2, 3, 4, 5 };
    Collections.shuffle(Arrays.asList(numbers));
    

    See it working online: ideone

    It uses the Fisher-Yates shuffle internally. This is an efficient shuffling algorithm that won’t give you duplicates.

    Related

    • Java's Collections.shuffle is doing what?
    • How to convert int[] to Integer[] in Java?
    • Arrays.asList() not working as it should?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have looked everywhere but have not found a problem similar to this. I
I have looked around on here but can not find out why this is
I have looked for this in several places but not necessarily found the right
Have looked quite hard for this answer but having no luck. I have 3
I have looked at questions like this , this , this and this ,
I have looked around on Google and StackOverflow for the answer to this question,
I have looked all over and cannot figure out why this code isn't working.
I have looked but have not found a specific answer for changing just one
I have looked thoroughly, but I have not found the solution (or perhaps I
I have an array of indices [1 ... 20]. The first 4 elements of

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.