Thread: Storing A Repeat Loop In An Array

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

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 06:28 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.

4. Originally Posted by Junky
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.
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.

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.

6. Originally Posted by Junky
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.
How would I call it differently or did you mean make 100 new ones? When I tried calling it using different variables it just gave me the same number.

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.

Posting Permissions

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