-4

Possible Duplicate:
Java: generating random number in a range

I am using Math.random() in java. But always it returns less than 0. Such as 0.4454590405954

Is there a way to return meaningful numbers as what i want.

For example i want to return numbers which values are 0 to 100. How i can do it?

Community
  • 1
  • 1
hakiko
  • 27
  • 4
  • 3
    Multiply the result by 100... – daveb Apr 19 '12 at 16:11
  • if i multiply the number, result will a double number. but i want to generate integer type number. – hakiko Apr 19 '12 at 16:12
  • 1
    Dup of [Java: generating random number in a range](http://stackoverflow.com/questions/363681/java-generating-random-number-in-a-range) (I mis-cast my close vote) – Matt Ball Apr 19 '12 at 16:13

2 Answers2

2

You can use Random.nextInt(int n) object to return int 0 - 100

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

For example

Random random = new Random();
int myRandom = random.nextInt(101);
// do magic
Pau Kiat Wee
  • 9,485
  • 42
  • 40
2

Use the following to get an integer from 0 to 100:

Random r = new Random();
int x = r.nextInt(101); // from 0 (inclusive) to 101 (exclusive)
Michael Schmeißer
  • 3,407
  • 1
  • 19
  • 32