Results 1 to 9 of 9
  1. #1
    Space Ranger is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default 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. #2
    totj is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    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. #3
    Space Ranger is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Thank you.
    I'll give it a whirl

  4. #4
    Space Ranger is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default 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. #5
    totj is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    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. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    Quote Originally Posted by Space Ranger View Post
    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. #7
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Quote Originally Posted by totj View Post
    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. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

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

  9. #9
    Space Ranger is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default 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.

Similar Threads

  1. how to change a single index of a string
    By ftrengnr in forum New To Java
    Replies: 5
    Last Post: 11-26-2010, 04:08 AM
  2. Change of index in charArray
    By kovalensue in forum New To Java
    Replies: 19
    Last Post: 11-10-2010, 11:34 AM
  3. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  4. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •