Results 1 to 7 of 7
- 10-02-2010, 09:07 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
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
- 10-02-2010, 09:21 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,543
- Rep Power
- 11
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.
- 10-02-2010, 10:44 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
For a perfect plan of attack read this link.
kind regards,
Jos
- 10-03-2010, 03:21 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
Thank you Guys- pbrockways2 and Joash...
I had to several hours.. finally i got it to work!! :D
- 10-03-2010, 03:44 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,543
- Rep Power
- 11
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)?
- 10-03-2010, 08:20 AM #6
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
@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...
- 10-03-2010, 08:25 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
fill object of array with char
By begginer12 in forum New To JavaReplies: 1Last Post: 12-05-2009, 02:11 AM -
(Trying to) fill HSB (Hue|Saturation|Brightness) array
By courteous in forum New To JavaReplies: 1Last Post: 01-19-2009, 07:49 AM -
Using reflection to create, fill, and display an array
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:15 PM -
Array Fill Test
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:45 PM -
Using reflection to create, fill, and display an array
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:43 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks