I have an array list that looks like this.

8.1,6.5,4.4,3.2,1,8.9,5,1.4,0.1,1,8.7,6.2,4.3,3.2,3

I would like that my program selects every 5 numbers randomly.

for example to select

8.1,6.5,4.4,3.2,1,8.7,6.2,4.3,3.2,3

as you can see it selected 5 numbers from the begging and 5 more from the end

and saves them

Random random_method = new Random();

// loop for generation random number
for (int i = 0; i < array.size(); i++)

// generating random index with the help of
// nextInt() method
int index = random_method.nextInt(array.size());

System.out.println("Random Element is :" + array.get(index));

I'd like to help but I don't understand what you meant by "every" in this line:

I would like that my program selects every 5 numbers randomly.

To me that meant that we select a random number from the list then add 5 to the index for the next and so on. Since this step of 5 will be more than the array length we could wrap around and keep going.

Now if I look at your code I don't see you seed the RNG (random number generator) so you may get the same sequence on each run.

That's two issues and the first one is clarifying the requirement, the second is selecting a way to seed the RNG.

Still unclear. If the first random number is the last entry of the array, what's the rule to pick the next?
Also, "scope creep." Once we get our first set of numbers you can call the routine as many times as you want. Avoid scope creep for now.

This leaves us with questions:

  1. What are the rules?
  2. Do we need to seed the RNG? (I always do.)
  3. What is "all in order?"

I've written so many random pickers in the past but here, these sentences mean different things.

  1. I would like that my program selects every 5 numbers randomly.
  2. I would like that my program selects 5 numbers randomly.
  3. I would like that my program selects 5 unique numbers randomly from the set.

And so on. Still would seed the RNG.

As to the help with programming, we don't code till we understand what we need to do.

I have an arraylist 8.1,6.5,4.4,3.2,1,8.9,5,1.4,0.1,1,8.7,6.2,4.3,3.2,3,2.0,3.0,3.2,.1.5,3 so 8.1,6.5,4.4,3.2,1 is one node. 8.9,5,1.4,0.1,1 is another node. 8.7,6.2,4.3,3.2,3 is another node and lastly 2.0,3.0,3.2,.1.5,3 is the last node what I need is for the program to generate and save random nodes. example from those nodes randomly select for example 8.7,6.2,4.3,3.2,3 and 8.1,6.5,4.4,3.2,1. as you can see the nodes were selected randomly and the order of the points in the nodes were not change. Thank you!!

With your last reply it appears you want:

  • Given an array of N values, randomly select an index value of N-5 and return 5 values from the array starting at index value.

This means your code above needs a few tweaks.

Line 3 above would need to need to be 5 less than the array.size. Try:
int index = random_method.nextInt(array.size()-5);

Now the for loop might be:
for (int i = index; i < index + 5; i++) System.out.println(array.get(i);

Remember this is not exact code for your project/assignment. You may have to edit it to meet your final goal.

Let's alter the deal:

With your last reply it appears you want:

  • Given an array of values which have groups of 5, randomly select an index value of N and return 5 values from the array starting at index value.

This means your code above needs a few tweaks.

Line 3 above would need to need to find how many groups of 5. Try:
int index = random_method.nextInt(array.size() / 5) * 5;

Now the for loop might be:
for (int i = index; i < index + 5; i++) System.out.println(array.get(i);

Remember this is not exact code for your project/assignment. You may have to edit it to meet your final goal.

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.