10 Replies Latest reply on Mar 28, 2007 7:57 PM by 807599

# random numbers and bubble sort with array

I am slightly familiar with bubble sort, but we haven't ever done it with an array; more on that later . . .

first of all, how do I code these instructions correctly?

"Write a program that generates an array, named numbers, containing 20 random integers within the range 0 to 100. You can generate a single random integer in this range with the statement: [variable_name_here] = (int) (100 * Math.random());"

I entered this code, but there are red error lines of death under '(100 * Math.random())'.
``int random[] = new int [100 * Math.random()];``
what is wrong?
• ###### 1. Re: random numbers and bubble sort with array
what is wrong?
Why didn't you do what the description says?
``````int n = (int) (100 * Math.random()); // one random integer
int[] array = new int[100]; //array of length 100``````
Kaj
• ###### 2. Re: random numbers and bubble sort with array
what is wrong?
Why didn't you do what the description says?
``````int n = (int) (100 * Math.random()); // one random
integer
int[] array = new int[100]; //array of length 100``````
Kaj
well, thought I did just what the directions showed, but thanks for showing the way; still trying to understand this stuff

please check back here as I work through the bubble sort; thanks again for your help
• ###### 3. Re: random numbers and bubble sort with array
thanks Kaj, that worked great . .

new question: why I am getting only 0s for my random numbers?
``````int random [] = new int[100];

for (int i = 0; i < random.length; i++){
System.out.println(random);
} ``````
• ###### 4. Re: random numbers and bubble sort with array
Well, in the above code you have only made a for loop that writes out the different places in the array without adding anything INTO the array. Hence all the places in the array is empty.
• ###### 5. Re: random numbers and bubble sort with array
Because you are creating an array of length 100, but all values are set to zero. You must create a loop that assigns a random number to each index.

Kaj
• ###### 6. Re: random numbers and bubble sort with array
new question: why I am getting only 0s for my random
numbers?
``````int random [] = new int[100];

for (int i = 0; i < random.length; i++){
System.out.println(random);
}

Perhaps I misunderstood the assignment, but should your array not contain only 20 numbers?

If so, you should use this ``````
int random [] = new int[20];
It is strange that you get 0, but could it be that you
As I understand your assignment, you should only have 20 numbers, could it be that you have only placed 20 number in your array? If so, the remaining 80 will be set to 0 which is the default value of 0.

Check all your system outs to see if also the first ones are 0.
• ###### 7. Re: random numbers and bubble sort with array
ahh, good points everyone; yeah, it is only supposed to be 20 numbers long

So, do I set up the array, then randomly assign numbers 1-100 with a for loop?
• ###### 8. Re: random numbers and bubble sort with array
I am trying to assign values with a for loop, but this isn't working:
``````for (int i = 0; i < randomNums.length; i++){
randomNums[i] = int (100 * Math.random());
}``````
what do I need to fix?
• ###### 9. Re: random numbers and bubble sort with array
I think I got it, I just put () around int
• ###### 10. Re: random numbers and bubble sort with array
I am trying to assign values with a for loop, but
this isn't working:
``````for (int i = 0; i < randomNums.length; i++){
randomNums[i] = int (100 * Math.random());
}``````
what do I need to fix?
What is the problem? Compilation error when trying to convert from double to int?

It is alway easier to help when you specify what is it working, and which errors you get :)

This works for me:
``````public class RandomArray {
public static void main(String[] args) {
int [] randomNums = new int [20];

for (int i = 0; i < randomNums.length; i++){
randomNums[i] = (int) (100 * Math.random());
}

for (int i = 0; i < randomNums.length; i++) {
System.out.println(randomNums);
}
}
}

Output:
5
7
14
72
4
23
21
76
69
62
10
7
37
63
68
72
88
44
91
23 ``````