Solved

Randomise An Array of integers

Posted on 2002-03-11
9
215 Views
Last Modified: 2010-03-31
Hi, My problem is that, first of all I have an array that contains integer values. Now, what I want is to shuffle the contents in a random manner.

I tried using the random class and failed by far cause it appears that it allows a certain range that it can select from. I dont need to select any range. I just want to randomise the values that I already have in the array. Hope it is clear and understandable.

Thanx...!!
0
Comment
Question by:q_bic
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 9

Accepted Solution

by:
Venci75 earned 100 total points
ID: 6855726
then - try this:
for (int i = 0; i < arr.length; i++) {
  j = (new java.util.Random()).nextInt(); // random position
  int buff = arr[i];
  arr[i] = arr[j];
  arr[j] = buff;
}
0
 
LVL 27

Expert Comment

by:rrz
ID: 6855744
How about?

import java.io.*;
import java.util.*;
public class Tryrandom
{
public static void main(String args[])
{
 Object numbers[] = {"0","1","2","3","4","5","6","7"};
 ArrayList as = new ArrayList(Arrays.asList(numbers));
 Collections.shuffle(as);
 numbers = as.toArray();
 for (int i=0;i<8;i++)
        System.out.println("result["+i+"]= "+ numbers[i]);
}
}
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6855745
something like this would work:

          Vector v1 = new Vector();

          for (int i=0;i<100;i++) {
               v1.add(new Integer(i));
          }
         
          Collections.shuffle(v1);
         
          for (int i=0;i<100;i++) {
               out.println(v1.elementAt(i));
          }

0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6855746
damn, too slow ;(
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6855753
Hi,

> j = (new java.util.Random()).nextInt();
There is no need to create new Random instance each time. And don't let index to go out of range:

Before loop: Random random = new java.util.Random();

Inside loop: j = random.nextInt() % arr.length;

Igor
0
 
LVL 6

Expert Comment

by:gadio
ID: 6855968
Heres an option. This is pseudo code:

java.util.Random r = new java.util.Random();
Object numbers[] = {"0","1","2","3","4","5","6","7"};
for( int cnt = 0; cnt < number.length; cnt++ ) {
   int index = (int)((numbers.length - cnt - 1)* r.nextDouble() + 0.5);
   // do the swap
   int tmp = numbers[cnt];
   numbers[cnt] = numbers[cnt+1+index];
   numbers[cnt+1+index] = tmp;
}

This promis you the following things:
1. Time complexity of o(n).
2. Space complexity of o(n).
3. You will touch each cell of the array at least once.
0
 
LVL 9

Expert Comment

by:Venci75
ID: 6856005
bazarny -> yes - you are right - I forgot this:
j = random.nextInt() % arr.length;
but regarding the new Random() - I used it only for demonstration.

rrz - great suggestion!
0
 

Author Comment

by:q_bic
ID: 6859999
thanx for the great Idea.
0
 
LVL 6

Expert Comment

by:gadio
ID: 6863845
I am sorry to say that after you had accepted the answer, however, the solution by Venci75 is wrong in my opinion.

This:
j = random.nextInt() % arr.length;

is not so good since you may touch any cell, also the ones that you already shuffled. The probability that you will undo shuffles is not zero. This is why in my solution I used:

int index = (int)((numbers.length - cnt - 1)* r.nextDouble() + 0.5);
 
And the range is getting smaller each time - to the ones that were not touched yet.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now