# Random integers, random locations in array.

Page 1 of 1

## 9 Replies - 1064 Views - Last Post: 09 October 2011 - 03:59 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=250412&amp;s=bc4e36d9044645efa43cec6de26dcb83&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 xJoelx6690

Reputation: 0
• Posts: 7
• Joined: 02-October 11

# Random integers, random locations in array.

Posted 08 October 2011 - 01:32 PM

Write a program that generates 5 unique random numbers, from 1 to 100
inclusive. Your program is to put each number twice into an array of
10 in random locations.

I'm having trouble with the last part - putting the number into an array of 10 random locations. I have it looping every unique random number twice. All I need to do is put those numbers in random locations. Any help would be appreciated.

```File Edit Options Buffers Tools Java Help
import java.util.*;
public class Prog8 {
public static Scanner sc=new Scanner(System.in);
public static void main (String [] args){

int []list = new int[5];
int []location = new int[10];
for (int k = 0;k<5;k++){
int number=(int)(Math.random()*100+1);
list[k]=number;
for(int l =0;l<2;l++){
list[l]=number;
l=(int)(Math.random()*10+1);
location[l]=list[l];
boolean hope = false;
while (!hope){
for (int i = 0;i<10;i++){
if (i!=l){
location[l]=i;
list[l]=number;
hope = true;

//System.out.println(l);
}
else if (i==location[l]){
hope = false;
}
}
}

System.out.println(list[l]);
}
}
}

}

```

Is This A Good Question/Topic? 0

## Replies To: Random integers, random locations in array.

### #2 GregBrannon

• D.I.C Lover

Reputation: 2245
• Posts: 5,329
• Joined: 10-September 10

## Re: Random integers, random locations in array.

Posted 08 October 2011 - 01:41 PM

Mmmm. Deja vu. This problem sounds familiar . . . . Oh well.

Every time I run it, I get an ArrayIndexOutOfBoundsException at line 16. That also sounds familiar. The index of list[] can only be 0 - 4, yet you're allowing it to be larger than that due to the assignment at line 15. The index of list[] cannot be the same as the index of location[], won't work.

Oh! And the index of location[] has to be 0 through 9.

This post has been edited by GregBrannon: 08 October 2011 - 01:43 PM

### #3 xJoelx6690

Reputation: 0
• Posts: 7
• Joined: 02-October 11

## Re: Random integers, random locations in array.

Posted 08 October 2011 - 04:27 PM

How can I assign the random numbers to random locations?

### #4 GregBrannon

• D.I.C Lover

Reputation: 2245
• Posts: 5,329
• Joined: 10-September 10

## Re: Random integers, random locations in array.

Posted 08 October 2011 - 04:36 PM

You're on the right track, but when you have 5 values that can be assigned to 10 possible locations, the array indices for the array of values and the array of locations cannot be the same. Change the indices to be different and within their allowed ranges.

Perhaps if you thought about how it would be done if the locations were not random you'd see a method to accomplish it. How would you assign 5 values to 2 each of 10 locations? The two locations could be successive, or the second might be some multiple of the first. How would you do that?

### #5 xJoelx6690

Reputation: 0
• Posts: 7
• Joined: 02-October 11

## Re: Random integers, random locations in array.

Posted 08 October 2011 - 06:09 PM

I'm still not too sure. I've put the rest of the code aside and just tried to make it so that the random number is put into an array but not more than once. I'm now struggling with that. I've tried a number of different things, but this is what I have now:

```int []list = new int[5];
int []location = new int[10];
for (int i = 0;i<10;i++){
int rand = (int)(Math.random()*9);

if (rand){
location[i]=rand;
}
else ;

System.out.println(location[i]);

}
}
}

```

This post has been edited by xJoelx6690: 08 October 2011 - 06:10 PM

### #6 GregBrannon

• D.I.C Lover

Reputation: 2245
• Posts: 5,329
• Joined: 10-September 10

## Re: Random integers, random locations in array.

Posted 08 October 2011 - 06:29 PM

Good idea to fallback and regroup. I suggest you fallback even further. Try to

1. Create an array to hold 5 integer values,
2. Generate 5 random integer values,
3. Store the 5 random values in the array, and
4. Print the 5 values stored in the array.

You shouldn't need an if statement, so put that aside for now.

### #7 xJoelx6690

Reputation: 0
• Posts: 7
• Joined: 02-October 11

## Re: Random integers, random locations in array.

Posted 08 October 2011 - 07:23 PM

I was able to do that quite simply:

``` int []list = new int[5];
int []location = new int[10];
for (int i = 0;i<5;i++){
int rand = (int)(Math.random()*9);
list[i]=rand;
System.out.println(list[i]);

```

### #8 GregBrannon

• D.I.C Lover

Reputation: 2245
• Posts: 5,329
• Joined: 10-September 10

## Re: Random integers, random locations in array.

Posted 09 October 2011 - 02:44 AM

Great! I hope you've moved on from there, but if you're waiting for a push:

1. Change the range of the 5 random values to 1 to 100,
2. As the 5 random values are generated, place each value twice in a 10-element array. Place the two values next to each other or apart by some interval, it doesn't matter, but all 10 locations have to be filled, and each of the 5 values can only be in the 10-member array twice. Then,
3. Print out the 10-member array.

Hints: Placing each random value next to itself in the 10-member array should not require an 'if' statement. Placing each value in the 10-member array separated by some interval MAY require an 'if' statement.

The 'if' statements I've seen you write are not so good. The basic form of an if statement is:
```if ( condition )
(
// do something here when condition is true
}
```

The condition must evaluate to a boolean true or false. Adding an 'else' to an if statement can be as simple as:
```if ( condition )
(
// do something here when condition is true
}
else
{
// do this code when condition is false
}
```

Notice the 'else' does not have to be followed by another if statement. The else CAN be followed by another if, but it doesn't have to be.

When you think you've added the above functionality, test it to be sure, then post the result. If you're anxious to move on, the next steps are to

1. Generate a random number 0 through 9
2. Place each of the 5 random numbers at a random location in the 10-member array only once, ensuring that the random location chosen has not already been filled.

### #9 xJoelx6690

Reputation: 0
• Posts: 7
• Joined: 02-October 11

## Re: Random integers, random locations in array.

Posted 09 October 2011 - 03:33 PM

Here's what I get, but I'm not sure if it's right because the element in the array is repeated:

```int []list = new int[5];
int []location = new int[10];

for (int i = 0;i<5;i++){

int rand = (int)(Math.random()*100+1);

for (int j =0;j<2;j++){
location[i]=rand;
System.out.println(location[i]);
System.out.println(i);
}
}

```

I understand that i is only in the range from 0 to 4. I do not know how to print each random value twice a different way and I believe this may cause further complications.

### #10 GregBrannon

• D.I.C Lover

Reputation: 2245
• Posts: 5,329
• Joined: 10-September 10

## Re: Random integers, random locations in array.

Posted 09 October 2011 - 03:59 PM

Here's what you were trying to do (I think):

1. Change the range of the 5 random values to 1 to 100,
2. As the 5 random values are generated, place each value twice in a 10-element array. Place the two values next to each other or apart by some interval, it doesn't matter, but all 10 locations have to be filled, and each of the 5 values can only be in the 10-member array twice. Then,
3. Print out the 10-member array.

Here's what you did:

1. Done!
2. No.
3. No.

The purpose of #3 was to test your code and to show that you placed the 5 random numbers 2 times each in the 10-element array. To accomplish #3 correctly, you'd start a new loop like:
```        for ( int i = 0; i < 10; i++ )
{
System.out.println( location[i] );
}
```

If you were to add that loop, you'd get something like:
number1
number2
number3
number4
number5
0
0
0
0
0

Why zeros? Because you never assigned any of the random numbers to location[5] - location[9]. To add the random numbers next to each other, you could simply use the code
```location[i] = number1;
location[i + 1] = number1;
```

With a little imagination, you can vary the assignments so that they are not next to each other. This is what you'll do eventually to locate the random numbers twice in the 10-member array.

Give it some more thought, and move to the next steps when you're ready.