Post Reply 
 
Thread Rating:
  • 1 Votes - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get random numbers
12-14-2010, 08:31 AM
Post: #1
Get random numbers
public class MyTest {
public static void main(String args[]) {
new MyTest().doit();
}
public void doit() {
java.util.Random rand = new java.util.Random();
// random integer 0 to 10
int max = 10;
for (int i=0; i<10; i++) {
System.out.println(rand.nextInt(max + 1));
}
// random double 0 to 10
double maxd = 10;
for (int i=0; i<10; i++) {
System.out.println(maxd * rand.nextDouble());
}
}
}



The output

4
0
0
10
10
4
10
10
6
10
8.13013058393518
0.6631286078928067
4.382003543427801
7.2768144451559795
7.312852816962123
8.69257797289748
2.4967782036871657
4.451145854389913
1.82517092998838
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:



Send your comments, Suggestions or Queries regarding this site at info@tutorials4u.net

Copyright © 2010 Tutorials4u.net All Rights Reserved