hi
below code is an example.I hav couple of questions
1)The highlighted bold red code(myParrot[0]...)can it be simplifyed?
2)Can i use random to generate a given set of numbers?How?

``````public class Bird{
public static void main(String[] args) {

int[] myParrot = new int[3];
[B]myParrot[0]=1;
myParrot[1]=2;
myParrot[2]=3;[/B]

for (int feature=0; i<myParrot.length; feature++) {
System.out.print(myParrot[feature]);
System.out.print(" ");
}
System.out.println();
}
}``````
4
Contributors
16
Replies
17
Views
6 Years
Discussion Span
Last Post by NormR1
Featured Replies
• 1

int[] myParrot = {1,2,3}; // simpler You can use Random to generate random numbers. I donlt kniw what you mean by "a given set of numbers"

int[] myParrot = {1,2,3}; // simpler

You can use Random to generate random numbers. I donlt kniw what you mean by "a given set of numbers"

Thanks James

ok i came up with the below code.Another question
1)Anyway of prevent the random number from displaying zeros?

``````public class Bird{
public static void main(String[] args) {

int[] myParrot = new int[3];
for (int i=0;i<3;i++){
myParrot[i]=(int)(Math.random()*3);
}

for (int i=0; i<myParrot.length; i++) {
System.out.print(myParrot[i]);
System.out.print(" ");
}
System.out.println();
}
}``````

Edited by Valiantangel: n/a

Do you mean you want random numbers 1-3 rather than 0-3? In that case, something like:
myArray= 1 + (int)(Math.random()*2);

Mmmmmmmm i mean the output from displaying 0.

Anyone can help? Creating a random number with no zero output?

Can you post the code that is generating a random number with the value of 0

Ok here is the code

public class Bird{
public static void main(String[] args) {

int[] myParrot = new int[3];
for (int i=0;i<3;i++){
myParrot=(int)(Math.random()*3);
}

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

Its ok i solved it MYSELF.

the random() method from the Math class returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
try using one of the the Random class methods instead
Here's an example:
http://www.javapractices.com/topic/TopicAction.do?Id=62

EDIT: Just noticed that you solved it on your own, congrats :)

Edited by zeroliken: n/a

Hey thats a good link.I will remember but for now i thik math.random() will do.

Could you show the code that solved the problem?

Did you test it well?

Edited by NormR1: n/a

Thanks zeroliken, i will hav a look
NormR1 below is the code.
Also i got 1 question.
1.What if i like to change the random output to hav a negative number, like -1?

``````public class Bird{
public static void main(String[] args) {

int[] myParrot = new int[3];
for (int i=0;i<3;i++){
myParrot[i]=(int)(Math.random()*3)+1;
}

for (int i=0; i<myParrot.length; i++) {
System.out.print(myParrot[i]);
System.out.print(" ");
}
System.out.println();
}
}``````

What is the range of numbers you want?
If you want to skip over 0 then you will have to write special code to select a number from two ranges.

no i dont want any range.just wondering if the negative sign can mirror the above positive output.
Lets say the above ouput gave 12345 can i hav a code to display a negative output -1-2-3-4-5 ?

Never mind i solved it again.Thanks God and thanks guys and gals:)

The random() method returns a number in a small range. You can change its range by adding to it and by multiplying it by some numbers.

Edited by NormR1: n/a