The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

home work ..

hey all how are you ?

a little problem I have , well I have to make an array which generates 50 numbers from 0 to 999 which I can manage that the problem lies with the part were the numbers generatet cant be the same here is what ive done so far

importjava.util.Random;publicclass ArrayRandom{publicstaticvoid main(String args[]{Random r =newRandom();int temp;int arr[]=newint[50];for(int i =0; i <50; i++){
temp = r.nextInt(999)+1;// this will give you a random number between 0 to 999.
arr[i]= temp;//here we know that the number is unique, so we can put it inside arr[i]}for(int i =0; i <50; i++){System.out.print(arr[i]+" ");}}}

Re: home work ..

Since this is homework, you're probably not allowed to use classes and methods available in the standard API which might make this easier.

Think how you would do this without a computer. Say you have a sheet of paper ruled with 50 lines. You call a friend and ask him/her to give you a number between 0 and 999. How do you decide whether that number can be written into the next blank line or has to be rejected because it's a duplicate? How do you know when to stop calling friends for more numbers?

Oh, and random.nextInt(999) doesn't give you a random number between 0 to 999. Read the API more carefully, paying special attention to the words inclusive and exclusive.

Re: home work ..

Well, I've cross posted on this forum before because I got impatient and days went by with no response. Also, I often update posts and the forum only shows original post dates, not edit post dates, as the last post date.

Also, it almost seems that you're spending more time reporting cross posters than you are asking for and/or giving help.