Java: How to randomly go through an array?
NickName:thisisdee Ask DateTime:2011-09-14T10:34:49

Java: How to randomly go through an array?

I have an array of size x and I need to go through the list randomly but getting to each element once. What is the most efficient way to do this?

Copyright Notice:Content Author:「thisisdee」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/7410677/java-how-to-randomly-go-through-an-array

Answers
ghostbust555 2011-09-14T02:37:02

What you are looking for is shuffle\n\ntry this-\n\n// Create a list\nList list = new ArrayList();\n\n// Add elements to list\n\n// Shuffle the elements in the list\nCollections.shuffle(list);\n\n// Create an array\nString[] array = new String[]{\"a\", \"b\", \"c\"};\n\n// Shuffle the elements in the array\nCollections.shuffle(Arrays.asList(array));\n",


Mahesh 2011-09-14T02:37:09

Just shuffle the array and then iterate over it.\n\nCollections.shuffle(Arrays.asList(yourArrayReference));\n",


aykutfirat 2015-03-20T02:28:29

Here is a time and space efficient way to do it. \n\nimport java.util.Enumeration;\nimport java.util.Random;\n\npublic class RandomPermuteIterator implements Enumeration<Long> {\n int c = 1013904223, a = 1664525;\n long seed, N, m, next;\n boolean hasNext = true;\n\n public RandomPermuteIterator(long N) throws Exception {\n if (N <= 0 || N > Math.pow(2, 62)) throw new Exception(\"Unsupported size: \" + N);\n this.N = N;\n m = (long) Math.pow(2, Math.ceil(Math.log(N) / Math.log(2)));\n next = seed = new Random().nextInt((int) Math.min(N, Integer.MAX_VALUE));\n }\n\n public static void main(String[] args) throws Exception {\n RandomPermuteIterator r = new RandomPermuteIterator(100);\n while (r.hasMoreElements()) System.out.print(r.nextElement() + \" \");\n }\n\n @Override\n public boolean hasMoreElements() {\n return hasNext;\n }\n\n @Override\n public Long nextElement() {\n next = (a * next + c) % m;\n while (next >= N) next = (a * next + c) % m;\n if (next == seed) hasNext = false;\n return next;\n }\n}\n",


More about “Java: How to randomly go through an array?” related questions

Java: How to randomly go through an array?

I have an array of size x and I need to go through the list randomly but getting to each element once. What is the most efficient way to do this?

Show Detail

How to go through an unknown number of quotes randomly without repeating

I have a small snippet of Javascript where I rotate through a list of quotes in order from beginning to end. However, I want to randomly go through the list (instead of in order), without repeating

Show Detail

Java maze game-- How to not go through the wall

So for a class project, I'm making a solvable maze game (in Java). I can randomly generate and display the maze without a problem, and the same goes for the user/player's representation. However,...

Show Detail

Iterate through an array randomly but fast

I have a 2d array that I need to iterate through randomly. This is part of an update loop in a little simulation, so it runs about 200 times a second. Currently I am achieving this by creating a ar...

Show Detail

How to randomly iterate through an array once, and then repeatedly iterate in that order

This is my array: arr = [0, 1, 2, 3, 4, 5] I want to randomly iterate through it the first time and then iterate through it in the same (random) order each time after that. How would I do it? The o...

Show Detail

Go through Array in O(logn) running time?

Is it possible to go through a boolean array to find a false value in O(logn) running time? The array's indices run from 0 to n-1. If it is, how would we do it in java? Pseudo code is fine.

Show Detail

Java move a file randomly

Hi so I'm working on an app and at the moment I have code that downloads a bunch of zip files to /sdcard/RAND I have it get all the files into a string array and now i want to move one of those zips

Show Detail

How do you randomly iterate through a 2D array with no duplicates?

You would normally iterate through a 2D array with two for loops: For i = 0 to 5 For j = 0 to 5 Console.Writeline(Arr(i,j)) Next Next I need to iterate through a 2D array (with square ...

Show Detail

Randomly generated numbers in a basic array

I simply need to know what i should do to make so that a basic array is filled with randomly generated numbers. now i know how to do that, what i don't know how to to do is to make it so that the

Show Detail

PHP How to loop randomly through an array of placeholders

I have a search input in my wordpress site, which I want to display different placeholdes each time the page been refreshed. So I will have an array of the different values, the function that will ...

Show Detail