random number help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • irondude56
    New Member
    • Jan 2007
    • 3

    #1

    random number help

    im trying to create a list of 20 random numbers that dont repeat the same number. the first loop is good but im having trouble with the imbedded statement it looks something like this

    for (int index =0; index < arraysize; index++)
    {
    prizeNumbers[index] = Math.abs(ran.ne xtInt() %500 + 1);

    while ( prizeNumber[index] != prizeNumber[0] threw prizeNumber[index-1])
    {
    }

    }
    the underlined is incorrect but thats the idea im going for can i get some help with this?
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    What language are you using? This thread would be more appropriate in a different forum.

    Comment

    • irondude56
      New Member
      • Jan 2007
      • 3

      #3
      oh java i thought i was posting in java tech section can i get a link im new to this forum havingt trouble finding things

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        The Java forum is here. I'll move the thread there now.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by irondude56
          im trying to create a list of 20 random numbers that dont repeat the same number. the first loop is good but im having trouble with the imbedded statement it looks something like this

          for (int index =0; index < arraysize; index++)
          {
          prizeNumbers[index] = Math.abs(ran.ne xtInt() %500 + 1);

          while ( prizeNumber[index] != prizeNumber[0] threw prizeNumber[index-1])
          {
          }

          }
          the underlined is incorrect but thats the idea im going for can i get some help with this?
          Is there any restriction on the numbers generated? Can they be just any integers?

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Alright then. This will produce numbers below 500 but I'm sure you know how to change it to another limit

            Code:
            import java.util.*;
            public class Rand {
            	public static void main(String... args) {
            		final int MAX_NUMBER = 500;
            		int[] randomInts = new int[20];
            		for(int i = 0; i < randomInts.length; i++) {
            			int x = (int)(Math.random()*MAX_NUMBER);
            			while(contains(randomInts, x)) {
            				x = (int)Math.random()*MAX_NUMBER;
            			}
            			randomInts[i] = x;
            		}
            
            		System.out.println(Arrays.toString(randomInts));
            
            	}
            	public static boolean contains(int[] array, int val) {
            		for(int i : array) {
            			if(i == val) {
            				return true;
            			}
            		}
            		return false;
            	}
            }
            Is that what you want?

            Comment

            • irondude56
              New Member
              • Jan 2007
              • 3

              #7
              Originally posted by r035198x
              Alright then. This will produce numbers below 500 but I'm sure you know how to change it to another limit

              Code:
              import java.util.*;
              public class Rand {
              	public static void main(String... args) {
              		final int MAX_NUMBER = 500;
              		int[] randomInts = new int[20];
              		for(int i = 0; i < randomInts.length; i++) {
              			int x = (int)(Math.random()*MAX_NUMBER);
              			while(contains(randomInts, x)) {
              				x = (int)Math.random()*MAX_NUMBER;
              			}
              			randomInts[i] = x;
              		}
              
              		System.out.println(Arrays.toString(randomInts));
              
              	}
              	public static boolean contains(int[] array, int val) {
              		for(int i : array) {
              			if(i == val) {
              				return true;
              			}
              		}
              		return false;
              	}
              }
              Is that what you want?

              :' ) yes thank you

              Comment

              Working...