-
February 2nd, 2016, 10:47 AM
#1
Help with random number?
hi i am trying out random numbers but stuck on something, please can someone help?
i have got a simple program that gives out random names, when i try to output several at the same time they come out the same. can i change it?
Code:
package coral;
public class namegen {
public static void main(String args[]) {
String[]names;
names = new String[10];
names[0] = "ben";
names[1] = "john";
names[2] = "chris";
names[3] = "matt";
names[4] = "jack";
names[5] = "alex";
names[6] = "patric";
names[7] = "sam";
names[8] = "mike";
names[9] = "andy";
int num = (int) (Math.random()*10);
System.out.println("My name is " + names[num]);
System.out.println("My name is " + names[num]);
System.out.println("My name is " + names[num]);
System.out.println("My name is " + names[num]);
}
}
My name is alex
My name is alex
My name is alex
My name is alex
-
February 8th, 2016, 08:43 PM
#2
Re: Help with random number?
they come out the same. can i change it?
Was this posted and answered on another forum?
Norm
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|