0

I am trying to write a program that needs a Array list with the numbers 0-25 randomly organized, I'm not sure how I'm supposed to approach this, because Math.random() doesn't give it a random place and it might cause repeating numbers... I'm not really concerned with the efficiency I just need it to work.

Keeawaop
  • 13
  • 1
  • Hint: select an array index at random – D.B. Apr 28 '18 at 16:15
  • 1
    Use an ArrayList with the shuffle() method. – camickr Apr 28 '18 at 16:17
  • 1
    Possible duplicate of [Random shuffling of an array](https://stackoverflow.com/questions/1519736/random-shuffling-of-an-array) – J-Alex Apr 28 '18 at 16:18
  • https://stackoverflow.com/questions/16112515/how-to-shuffle-an-arraylist, https://stackoverflow.com/questions/4228975/how-to-randomize-two-arraylists-in-the-same-fashion – J-Alex Apr 28 '18 at 16:19

3 Answers3

2

Try this code :-

import java.util.*;

public class test {
    public static void main(String[] args) {

        // Declaring Array List
        ArrayList<Integer> values = new ArrayList<>();

        // Adding Elements to  Array List. 
        for (int i=0;i<=25;i++) {
            values.add(i);
        }

        // Printing original Array List
        System.out.println(values);

        // Shuffling Array List . Each time you get a random value. Try Running Program few times.
        Collections.shuffle(values);

        // Printing shuffled Array List
        System.out.println(values);
    }
}

For more details on shuffle function.

anoopknr
  • 3,177
  • 2
  • 23
  • 33
1

I think that's what you want try

Exmple :

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

class try2
{
  public static void main(String args[])
  {
    int[] solutionArray = { 0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,13,14, 15, 16, 17, 18, 19, 
        20, 21, 22, 23, 24, 25};

    shuffleArray(solutionArray);
    for (int i = 0; i < solutionArray.length; i++)
    {
      System.out.print(solutionArray[i] + " ");
    }
    System.out.println();
  }

  // Implementing Fisher–Yates shuffle
  static void shuffleArray(int[] ar)
  {
    // If running on Java 6 or older, use `new Random()` on RHS here
    Random rnd = ThreadLocalRandom.current();
    for (int i = ar.length - 1; i > 0; i--)
    {
      int index = rnd.nextInt(i + 1);
      // Simple swap
      int a = ar[index];
      ar[index] = ar[i];
      ar[i] = a;
    }
  }
}

Output :

5 15 1 6 23 8 10 14 11 0 12 24 3 7 16 4 20 25 9 22 2 19 17 18 21 13 
Abdo Bmz
  • 632
  • 1
  • 11
  • 24
1

So, this might not be the best way to do it, but it is simple and it works well...

ArrayList<Integer> rand=new ArrayList<Integer>();
ArrayList<Integer> atoz=new ArrayList<Integer>();
    for(int a=0;a<26;a++) {
        atoz.add(a);
    }
for(int b=0;b<26;b++) {
    int temp=(int)(Math.random()*(26-b));
    rand.add(atoz.get(temp));
    atoz.remove(temp);
    }   

So the rand ArrayList is the one with the randomized places and the atoz ArrayList started with the Integers 0 to 25 but is now empty.