Hello everyone, I need help with my program. The program has to randomize the array indexs. I am having a huge issue with this:

import java.util.Random; //to generate random #'s

public class Array
{
   public static void printIntArray(int[] myParamVar)
   { 
    int LOCATION = 0;   

    while(LOCATION < myParamVar.length)
     {
       System.out.print("In LOCATION " + LOCATION + " ");
       System.out.print("Susie has written ");
       System.out.println(myParamVar[LOCATION]);

       LOCATION = LOCATION + 1; 
       }
      return;
     }

        public static void main (String[] args )
        {
          int[] myParamVar = {59, 39, 16, 10, -1}; 
          int random = (int)(4.9999 * Math.random());

          for(int i =0; i < myParamVar.length; i++)
          {

          }
          System.out.println(myParamVar);
          printIntArray(myParamVar);
          }   
}

It would be nice if someone point me into the right direction !!

Recommended Answers

All 3 Replies

The program has to randomize the array indexs.

What does that sentence mean??? If your program generate a random index of an array, what's next?

Your program is importing Random class, but you instead use Math.random() method to generate a number? What you need to do is to follow the Random API doc. To generate an integer, you would do it as follows:

Random rand = new Random();  // create an instance of Random class
int randomNumber = rand.nextInt(MAXIMUM_NUMBER);

// Above, the random number is generated from 0 up to MAXIMUM_NUMBER-1.
// In mathematical range notation, it is [0, MAXIMUM_NUMBER).

Then what do you want to do next with the random number???

It seems that nobody here understands what you mean by "randomize the array indexs".
Java array indexes go from 0 to (length-1) as a strict integer sequence. The language does not allow you to change that in any way, let alone "randomise" it.
Can you re-state your requirement in a way that makes sense?

ps Java naming conventions reserve all-upper-case names for constants. Variable names should be in camel case (eg myParamVar)

Do you mean you want to scramble the order of things in an array, like shuffling a deck of cards? In that case go through the array a couple of time, swapping each element with one that has been randomly chosen from elsewhere in the array. That ought give you a randomized order.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.