Results 1 to 7 of 7
  1. #1
    John_shok is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default Fill array with random number, but elements cannot be duplicated

    say we have,
    int a[]=int a[5];

    how do we fill 5 elements i.e 1,2,3,4,5 randomly in a[i](where i=0..4). using for loop.
    example
    a[0]=4
    a[1]=2;
    a[2]=3;
    a[3]=5
    a[4]=1

    I was practicing arrays. I came up with this thought but i couldn't figure it out. I can fill random number using for loop, but random number generates duplicate number.I was trying to figure out without duplicate number..
    Thank you

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    how do we fill 5 elements i.e 1,2,3,4,5 randomly in a[i](where i=0..4). using for loop.

    What you need is a "plan of attack".

    How would you do this by hand? I mean if you had a dice, a piece of paper, and a pencil how would go about generating a sequence of 5 numbers which lacked repetition?

    Once you have such a plan try turning each of the steps into Java. If you get stuck, say what you have done and what you are trying to do: ie the plan of attack you are using.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,533
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by pbrockway2 View Post
    What you need is a "plan of attack".
    For a perfect plan of attack read this link.

    kind regards,

    Jos

  4. #4
    John_shok is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    Thank you Guys- pbrockways2 and Joash...
    I had to several hours.. finally i got it to work!! :D

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Well done. Did you try getting random numbers and throwing them away if you have already seen them? Or did you follow Jos' link (which is the way the Java shuffle() method does it)?

  6. #6
    John_shok is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    @pbrock: whole day i was thinking about what u said..!! And I couldnt figure it out... So i i followed joash's link... imported Random class,
    1. filled the arrays using for loop.. serially
    2 used another for loop and used Durstenfeld's algorithm from joash;s link

    took me several hours ,, to get the wanted result;
    took even more hours too figure out the logic...

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,533
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by John_shok View Post
    @pbrock: whole day i was thinking about what u said..!! And I couldnt figure it out... So i i followed joash's link... imported Random class,
    1. filled the arrays using for loop.. serially
    2 used another for loop and used Durstenfeld's algorithm from joash;s link

    took me several hours ,, to get the wanted result;
    took even more hours too figure out the logic...
    Take a look at your JDK directory; there's a large file named src.zip stored there. Open it and read the file Collections.java and search for the shuffle algorithm. Check your own algorithm against it to see if you implemented it correctly.

    kind regards,

    Jos

Similar Threads

  1. fill object of array with char
    By begginer12 in forum New To Java
    Replies: 1
    Last Post: 12-05-2009, 02:11 AM
  2. (Trying to) fill HSB (Hue|Saturation|Brightness) array
    By courteous in forum New To Java
    Replies: 1
    Last Post: 01-19-2009, 07:49 AM
  3. Replies: 0
    Last Post: 04-23-2008, 08:15 PM
  4. Array Fill Test
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:45 PM
  5. Replies: 0
    Last Post: 04-14-2008, 08:43 PM

Posting Permissions

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