Results 1 to 7 of 7
- 07-22-2011, 05:03 AM #1
Storing A Repeat Loop In An Array
Hello, I have been trying to figure this out for a while and unfortunately I haven't found a solution to the problem. I set up a random number generator to repeat and I was wondering if there would be a way to store the repeats in an array so I wouldn't have to make 154 repeat loops.
-Thanks In Advanced
- 07-22-2011, 05:16 AM #2
I might have figured it out I'm going to continue reading but it looks like I need to use a "Enhanced for loop"
EDIT: Nope doesn't look like thats the right way.Last edited by anthropamorphic; 07-22-2011 at 05:28 AM.
- 07-22-2011, 05:21 AM #3
Your description is a bit vague. Are you asking how to generate N random numbers without getting any duplicates? If so you have 2 options.
If the range of random numbers is limited(eg 1-100) then you can place all the possible numbers in a List, shuffle it and poll N numbers from the front of the List.
If the range is exstensive then you can use a Set which does not allow duplicates. Keep generating random numbers and inserting them into the Set until the size of the set is N.
- 07-22-2011, 05:27 AM #4
No what I meant is I wanted to lets say generate 4 random numbers and I wanted to store each into an array by hooking a random number generator up to a repeat loop. (I really need to generate over a hundred but for the example I'm just going to say 4.) As I could go through the code and make over 100 different random number generators their seems like their has to be a faster way.
- 07-22-2011, 05:39 AM #5
Your post is still vague. If you need to generate 100 random numbers then you need to call the RNG 100 times. There is no quicker way to do it. Once you have generated the numbers you can o what you like with them: print, add, store whatever.
By the way all loops repeat. So saying "use a repeat loop" is redundant.
- 07-22-2011, 05:41 AM #6
- 07-22-2011, 05:57 AM #7
:sigh:
You are still not making sense. If you cannot convey what you are trying to do in a clear and unambiguous manner then how the heck is anyone supposed to provide an adequate answer. So far all I have been doing is guessing what you are trying to do. Once again if you want to generate N random numbers then yes you will need a loop and you will need to store each and every number in a unique memory location. Using N variables is unwieldy and non-dynamic. Using an array is better. Using a Collection such as a List is best.
Similar Threads
-
Storing array?
By LifeElixer in forum New To JavaReplies: 7Last Post: 04-14-2011, 06:10 AM -
Storing coefficients into an array
By shanus180 in forum New To JavaReplies: 4Last Post: 03-01-2011, 10:34 PM -
Need help with array and storing input
By Bewitched1 in forum New To JavaReplies: 8Last Post: 07-24-2010, 05:16 PM -
Storing in an Array
By Bascotie in forum New To JavaReplies: 10Last Post: 10-15-2009, 05:12 AM -
Repeat while answer yes - do while loop
By AJ2009 in forum New To JavaReplies: 1Last Post: 01-10-2009, 06:19 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks