Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

Im using Eclipes Android. So here's my array1, and I want it to transfer to another array(array2) randomly. I've been working on it for hours but I can't get it right.

int array1[] = {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,26,27,28,29,30,
                31,32,33,34,35,36,37,38,39,40,
                41,42,43,44,45,46,47,48,49,50};

I want to transfer it to my new array, array2 randomly.

I'm still new to java and still learning.Thanks.

share|improve this question
1  
What have you tried so far? – bradimus Feb 9 at 15:36
    
yes, ive been working for 2-3hrs straight and it always crashes my app in the emulator. :( – Bobby Besande Casio Feb 9 at 15:38
    
First you copy the array, then you shuffle using Fisher-Yates: bost.ocks.org/mike/shuffle – fafl Feb 9 at 15:38
    
I've actually asked/answered a question about this (Fisher-Yates) stackoverflow.com/questions/35242740/… – Idos Feb 9 at 15:39
    
java eclipes class doesnt recognize functions. :< – Bobby Besande Casio Feb 9 at 15:40
Random randomGenerator = new Random();
j = 0;
for (int idx = 1; idx <= 10; ++idx){
  {
   int randomInt = randomGenerator.nextInt(array1.length());
    array2[j] = array1[randomInt];
    j++;
  }
}

this is just an idea proceed accordingly.

share|improve this answer

Here's what I would suggest that you do to randomize an array:

for(int i = 0; i < array1.length; i++) {
    int random = (int)(Math.random() * 49 + 1);
    int temp = array1[random];
    array1[random] = array1[i];
    array1[i] = temp;
}

This should randomly shift values around. In each iteration, a random number's element will switch places with the iteration index's element. In you case, you'll have to copy the array into another array before doing the above code.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.