0

This is the question i got and how can i limit it to a range ? My code is below. Thanks

Question : Write a method call genRandom() to generate 5 Random integer numbers between 1 to 100 and print them on to the screen. Invoke genRandom() inside the main().

import java.util.Random;

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

        Random randomNum = new Random();

        for(int i = 0; i<5; i++){
            System.out.println("Random number is : " + randomNum.nextInt(5));
            }
    }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

5 Answers5

4

use 100 as argument in nextInt()

nextInt(100)

Update based on your comment

just create a new static method

public static int genRandom(){
  return new Random().nextInt(100);// or may be cache random instance
}
Srivathsan
  • 519
  • 4
  • 18
jmj
  • 237,923
  • 42
  • 401
  • 438
  • It says Invoke genRandom() inside the main(). how to do this ? – ŦhųLẩnå FệrñảnDö Oct 14 '12 at 06:31
  • could you please elaborate it little more ? – jmj Oct 14 '12 at 06:35
  • Thanks for your time friend. The question says this " Write a method call genRandom() to generate 5 Random integer numbers between 1 to 100 and print them on to the screen. Invoke genRandom() inside the main()." But i think i havent written a method called getRandom() – ŦhųLẩnå FệrñảnDö Oct 14 '12 at 06:36
  • @ThuLana FernanDo, it is porbably related with your other problem. otherwise, it would just work fine if you replace your '5' inside nextInt() with '100' – Jimmy Oct 14 '12 at 06:37
1

Write genRandom() in your RandomNumbers class and use as below:

    public class RandomNumbers{
       static Random randomNum = new Random();

       public static void main(String[] args){
          for(int i=0; i< 5; i++){
               System.out.println("Random number is : " + genRandom());
          }
       }

       public static int genRandom(){
          return randomNum.nextInt(100);
       }   
    }
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
0

nextInt generates random number between 0 to your given range value . If you give 5 it will generates random between the 5 . If you want random numbers between 1 to 100 you will give 100 in nextInt method .

Please read the java 6 api documentation http://docs.oracle.com/javase/6/docs/api/java/util/Random.html#nextInt(int) .

kannanrbk
  • 6,964
  • 13
  • 53
  • 94
0

You can try something like this:

package com.stackoverflow.works;

import java.util.Random;

public class RandomNumber {

    public static int genRandom(int randomRange) {
        return new Random().nextInt(randomRange);
    }

    public static void genRandom(int randomRange, int displayCount) {
        for (int i = 0; i < displayCount; i++) {
            System.out.println("Random Number [Iteration " + (i + 1) +"]: " + genRandom(randomRange));
        }
    }

    public static void main(String[] args) {
        int randomRange = 100; // You can change the limit if required
        int displayCount = 5; // You can decide the iteration count

        RandomNumber.genRandom(randomRange, displayCount);
    }

}

Output:

Random Number [Iteration 1]: 14
Random Number [Iteration 2]: 84
Random Number [Iteration 3]: 96
Random Number [Iteration 4]: 38
Random Number [Iteration 5]: 61
1218985
  • 7,531
  • 2
  • 25
  • 31
-1

You can use nextDouble() like below:

r = minValue + ((new Random()).nextDouble() * (maxValue - minValue));

where minValue, maxValue is the range you want your random number in.

Full Code

import java.util.Random;

class Main {

    int genRandom(int minValue, int maxValue) {
        return (int) (minValue + ((new Random()).nextDouble() * (maxValue - minValue)));
    }

    public static void main(String [] args){
        Main m = new Main();
        for(int i = 0; i<5; i++){
            System.out.println("Random Number is: " + m.genRandom(1, 100));
        }
    }
}

Sample Output:

Random Number is: 95
Random Number is: 89
Random Number is: 9
Random Number is: 73
Random Number is: 20
techfoobar
  • 65,616
  • 14
  • 114
  • 135