Results 1 to 7 of 7
  1. #1
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default 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. #2
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    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.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    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. #4
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

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

    Default

    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. #6
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

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

    Default

    :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

  1. Storing array?
    By LifeElixer in forum New To Java
    Replies: 7
    Last Post: 04-14-2011, 06:10 AM
  2. Storing coefficients into an array
    By shanus180 in forum New To Java
    Replies: 4
    Last Post: 03-01-2011, 10:34 PM
  3. Need help with array and storing input
    By Bewitched1 in forum New To Java
    Replies: 8
    Last Post: 07-24-2010, 05:16 PM
  4. Storing in an Array
    By Bascotie in forum New To Java
    Replies: 10
    Last Post: 10-15-2009, 05:12 AM
  5. Repeat while answer yes - do while loop
    By AJ2009 in forum New To Java
    Replies: 1
    Last Post: 01-10-2009, 06:19 PM

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
  •