By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,034 Members | 820 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

random number help

P: 3
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.nextInt() %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?
Jan 20 '07 #1
Share this Question
Share on Google+
6 Replies


Ganon11
Expert 2.5K+
P: 3,652
What language are you using? This thread would be more appropriate in a different forum.
Jan 20 '07 #2

P: 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
Jan 20 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
The Java forum is here. I'll move the thread there now.
Jan 20 '07 #4

10K+
P: 13,264
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.nextInt() %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?
Jan 20 '07 #5

10K+
P: 13,264
Alright then. This will produce numbers below 500 but I'm sure you know how to change it to another limit

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class Rand {
  3.     public static void main(String... args) {
  4.         final int MAX_NUMBER = 500;
  5.         int[] randomInts = new int[20];
  6.         for(int i = 0; i < randomInts.length; i++) {
  7.             int x = (int)(Math.random()*MAX_NUMBER);
  8.             while(contains(randomInts, x)) {
  9.                 x = (int)Math.random()*MAX_NUMBER;
  10.             }
  11.             randomInts[i] = x;
  12.         }
  13.  
  14.         System.out.println(Arrays.toString(randomInts));
  15.  
  16.     }
  17.     public static boolean contains(int[] array, int val) {
  18.         for(int i : array) {
  19.             if(i == val) {
  20.                 return true;
  21.             }
  22.         }
  23.         return false;
  24.     }
  25. }
Is that what you want?
Jan 20 '07 #6

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

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class Rand {
  3.     public static void main(String... args) {
  4.         final int MAX_NUMBER = 500;
  5.         int[] randomInts = new int[20];
  6.         for(int i = 0; i < randomInts.length; i++) {
  7.             int x = (int)(Math.random()*MAX_NUMBER);
  8.             while(contains(randomInts, x)) {
  9.                 x = (int)Math.random()*MAX_NUMBER;
  10.             }
  11.             randomInts[i] = x;
  12.         }
  13.  
  14.         System.out.println(Arrays.toString(randomInts));
  15.  
  16.     }
  17.     public static boolean contains(int[] array, int val) {
  18.         for(int i : array) {
  19.             if(i == val) {
  20.                 return true;
  21.             }
  22.         }
  23.         return false;
  24.     }
  25. }
Is that what you want?

:' ) yes thank you
Jan 20 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.