# Thread: Array of random numbers ...

1. Member
Join Date
Nov 2010
Location
wales
Posts
13
Rep Power
0

## 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

Java 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 : )

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
Java 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.

3. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
785
Rep Power
12
only a little note:

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

4. Member
Join Date
Nov 2010
Location
wales
Posts
13
Rep Power
0
what is an api doc everyone keeps telling me this thanx for you'r help aswell im new to java : )

5. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
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.

6. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
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.

7. Member
Join Date
Nov 2010
Location
wales
Posts
13
Rep Power
0
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

8. Senior Member
Join Date
May 2010
Posts
436
Rep Power
11
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.

9. Member
Join Date
Nov 2010
Location
wales
Posts
13
Rep Power
0
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

10. Senior Member
Join Date
May 2010
Posts
436
Rep Power
11
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. Member
Join Date
Nov 2010
Location
wales
Posts
13
Rep Power
0
ok i have done it im sorry for this didnt realize the outcome was like this

12. Member
Join Date
Nov 2010
Location
wales
Posts
13
Rep Power
0
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

13. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
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!

14. Member
Join Date
Nov 2010
Posts
3
Rep Power
0
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

15. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
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.)

16. Member
Join Date
Nov 2010
Location
wales
Posts
13
Rep Power
0
ha ha most probs lol :)

17. Member
Join Date
Nov 2010
Location
wales
Posts
13
Rep Power
0
Java 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

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•