# Array of random numbers ...

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-07-2010, 07:31 PM
shane1987
Array of random numbers ...
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

Code:

```import java.util.Random; public class ArrayRandom{     public static void main(String args[]         {             Random r = new Random();             int temp;             int arr[] = new int[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] + " ");                         }                 }```
if some one could help me this would be a great help thanx shane : )
• 11-07-2010, 07:54 PM
pbrockway2
Code:

```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]```

In between these lines you are expected to check to see that temp is a number that has not been generated before. Think about:

* how you are going to do that. (Imagine you are the computer and use a piece of paper to record the array values. Say the number was generated was 42. How would you decide if this was a new number?). When you have decided on a process you can start expressing it in code. It is quite possible that this may involve yet another for loop.

* What are you going to do if the number is not unique. Ie if you have seen it before.
• 11-07-2010, 07:55 PM
eRaaaa
only a little note:

Quote:

temp = r.nextInt(999) + 1; // this will give you a random number between 0 to 999.
this is not correct.
"the next pseudorandom, uniformly distributed int value between 0 (inclusive) and n (exclusive) from this random number generator's sequence"

nextInt(999) --> 0-998 + 1 = 1-999
nextInt(1000) --> 0-999
• 11-07-2010, 07:58 PM
shane1987
what is an api doc everyone keeps telling me this thanx for you'r help aswell im new to java : )
• 11-07-2010, 08:00 PM
Zack
• 11-07-2010, 08:46 PM
pbrockway2
You are looking for the documentation for the java.util.Random class (the classes are listed down the left hand side) to check what it says about nextInt().

If you replace 999 with 1000 you will get a number between 1 and 1000 inclusive. So you might want to something about the +1 as well.

You also might want to check whether the desired range is between 0 and 999 inclusive or 1 and 999 inclusive. Often this question gets asked using the latter range, although your case may be different.
• 11-07-2010, 09:44 PM
pbrockway2
Quote:

Write a program called Random50 that fills an array with fifty random values between 1 and 999

OK, so now we have the range. Are you clear about how to use nextInt() to get numbers in this range?

Once you are, you can think about the duplicates: how to detect them, and what to do once you find one.
• 11-07-2010, 10:54 PM
DarrylBurke
• 11-07-2010, 11:20 PM
DarrylBurke
• 11-07-2010, 11:29 PM
shane1987
why is this a problem im looking for someone to treat me like a thicko cuz thats what i am in java all the other treat me like im a pro
• 11-07-2010, 11:52 PM
curmudgeon
No one wants to waste time working out someone's problem when it's already been solved elsewhere in a cross-post. Ignoring this implies that the poster doesn't respect the time commitment made by the volunteers of the various forums. The best thing to do is to notify all if you've made cross-posts and where you've made them. This forum is no different from any of the others in this regard.
• 11-08-2010, 12:11 AM
shane1987
ok so if i wanna stay on this forum i just tell the rest i dont need there help yeh im sorry i will correct this now
• 11-08-2010, 12:13 AM
curmudgeon
Quote:

Originally Posted by shane1987
ok so if i wanna stay on this forum i just tell the rest i dont need there help yeh im sorry i will correct this now

No, all you need to do is notify all locations of every cross-post.
• 11-08-2010, 12:14 AM
shane1987
ok i have done it im sorry for this didnt realize the outcome was like this
• 11-08-2010, 09:11 AM
shane1987
Quote:

Originally Posted by pbrockway2
OK, so now we have the range. Are you clear about how to use nextInt() to get numbers in this range?

Once you are, you can think about the duplicates: how to detect them, and what to do once you find one.

Hi sorry about cross posting ive informed all other sited and I do apreichiate your help for me also im not clear about the nxt.Int()
so can we start again ? :o
• 11-08-2010, 09:55 PM
pbrockway2
We've figured out what the range is and - I hope - untangled the cross post mess as much as it can be.

If you think the approach may be any good, I would suggest going back to the two points I made in my first post. I'll repeat the first of them:

how you are going to do check that temp has not been seen before. (Imagine you are the computer and use a piece of paper to record the array values. Say the number was generated was 42. How would you decide if this was a new number?). When you have decided on a process you can start expressing it in code. It is quite possible that this may involve yet another for loop.

Notice that there's a question in there!
• 11-09-2010, 09:10 PM
razzle
Heya, I've got this exact same problem with my assessed work, I can generate the random numbers, etc. I'm just having a load of trouble wrapping my head around this checking business. At the moment I've got the idea of using a while loop inside the while loop generating the random numbers, with an if statement inside that testing but that's when I get stuck.

while (numbers[arrayPos] <= count)
{
if randNum != number[arrayPos]

}

now after the if statement I don't know what to put... i want it to check all the values first before I do something... Sorry to put this in your thread shane1987, but I think we might actually be in the same class :P
• 11-09-2010, 09:40 PM
pbrockway2
You really should start your own thread. In general terms you might need a variable whose value gets set by the setting loop to say whether the number is good. (Or another method that is passed the number and the array and returns a boolean value to say whether or not to add the number.)
• 11-09-2010, 11:33 PM
shane1987
ha ha most probs lol :)
• 11-09-2010, 11:44 PM
shane1987
Code:

```  1.       import java.util.Random;   2.       public class ArrayRandom   3.       {   4.       public static void main(String args[])   5.       {   6.       Random r = new Random();   7.         8.       int temp;   9.       int arr[] = new int[50];   10.       for(int i = 0; i < 50; i++)   11.       {   12.       13.       temp = r.nextInt(1000); // this will give you a random number between 0 to 999   14.         15.         16.       zeroStr = "000" //string with 3 zeroes.   17.         18.       int r = String.valueOf(array[i]).length(); // get the length of element i   19.         20.       if (r < 3) { // for digits less than 3   21.         22.       String temp = zeroStr.substring(0, 3 - r) + String.valueOf(array[i]); // concat the zeroes together with the digit   23.         24.       System.out.println(temp);   25.         26.       }   27.         28.       arr[i] = temp; //here we know that the number is unique, so we can put it inside arr[i]   29.       }   30.       for(int i = 0; i < 50; i++)   31.       {   32.       System.out.print(arr[i] + " ");   33.       }   34.       }   35.         36.         37.       }```

i no ive missed a step just wanted to no is this what you do to ensure the there is 3 digits at all time
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last