# Thread: Pick random array index and change the element value

1. Member
Join Date
Mar 2011
Posts
4
Rep Power
0

## Pick random array index and change the element value

Supposin' you have an array
{1,2,3,4,5,6,7,8,9,10}.
Also supposin' you want to randomly pick an index in that array and set the element of that random index to the value of 5.
Now, simply print out the number of the changed index and:
Lastly Supposin'
you then want to add up all the elementsof the changed array to get the sum.

example:
index 3 was randomly picked. the value of index 3's element is changed from 4 to 5.
When ran, the program then displays :
4 - 57

for the life of me i can't seem to find out how to choose a random index and manipulate its element.

I don't have any useful code for this so far, because i simply can't get past this whole 'random index' business.

Any help is greatly appreciated.
Last edited by Space Ranger; 03-14-2011 at 11:42 PM. Reason: used wrong number

2. Member
Join Date
Mar 2011
Posts
13
Rep Power
0
Random randnum = new Random(10);
yourarray[randnum] = 5;

then just do a for loop to run through the array and add the value of it to an integer i.e. sum += yourarray[counter];

3. Member
Join Date
Mar 2011
Posts
4
Rep Power
0
Thank you.
I'll give it a whirl

4. Member
Join Date
Mar 2011
Posts
4
Rep Power
0

## Still can't get it

Thanks for the help.
but i still can't seem to get this one going.
I've searched high and low and i still can't seem to get this lil bit of code to go.

for the sake of clarity, i should correct my former post.

if you have an array {1,2,3,4,5,6,7,8,9,10} and you want to pick a random index and then change the value of the element of that random index to 5 and then add all of the elements of the resultant array to get a sum.

example:
the code randomly selects index 3, which it then changes that element value to 5 and then prints out the index changed and then the sum of the array.
to get:
3 - 56 (i messed up both these number in the previous post).
i tried what was suggested, but no go.
I'm not necessarily looking for the code to be written for me. but a good hard shove in the right direction would be appreciated.
Thanks for putting up with me.
Last edited by Space Ranger; 03-21-2011 at 08:46 PM. Reason: bad spelling

5. Member
Join Date
Mar 2011
Posts
13
Rep Power
0
im still a little confused :o

10+9+8+7... = 55, and if you mean adding the array's new elements it would be
1+2+5+4+5... = 57, when you changed index 3 to 5

printing the index changed should be pretty simple, so ill try to help with the other problem...which i dont fully understand :p whats the specific problem? or any of the wrong output?

6. Originally Posted by Space Ranger
i tried what was suggested, but no go.
That provides zero information. What was suggested was exactly what you needed. If you didn't get it to work then you did something wrong. What did you do wrong? We have no idea since you did not provide relevant infirmation. Such as code, exceptions, incorrect output.

7. Senior Member
Join Date
Jan 2011
Location
Bangalore, India
Posts
102
Rep Power
0
Originally Posted by totj
Random randnum = new Random(10);
yourarray[randnum] = 5;
I'm sorry to say this is wrong and this is the reason why it is still not working. Have you ever tried printing the value of "randnum"? It is not an integer. It is an object. You cannot index an array using an object.

A much straight forward way will be
Java Code:
int randNum = (int)(Math.random()*10);
youarray[randNum] = 5;
PS : this is applicable in a case where you need to generate a number between 0 and 10. Which means, your array should be of size 11. If you want to do it on a 10 size array, then you can do something like this.
Java Code:
int randNum = ((int)(Math.random()*10))%9;
youarray[randNum] = 5;
But this is not such an efficient way to generate a random number because the probability of 1 is higher than other numbers.
So better refer to Random API and try nextInt(int n). I think this is a perfect solution.
Random (Java 2 Platform SE v1.4.2)

8. Yes well spotted. I missed that the code provided never called the nextInt method.
[slaps wrist]

9. Member
Join Date
Mar 2011
Posts
4
Rep Power
0

## Sorry. i'm fairly new to this stuff

I thank you all for your help.

the problem i had with the 'yourarray[randnum] = 5;' was that the compiler gave me an 'incompatible types' error.
i searched high and low to find out what i was doing wrong, but in the end, i simply gave up.
I'll definitely try Subith86's solution.

I'm not trying to be deliberately vague. the very heart of my little program is getting this random index operation going. until i do, i have no useful code to show all of you.

Thank you again for your help and patience.