0

i'm trying to read in two integer numbers from the console and then display a random integer number generated between these two numbers.

this is my code.

import java.awt.*;
import java.io.*;
import java.util.*;
import java.math.*;

public class Question8{

    public static void main(String[] args)
    {
        int first, second;

        Scanner myScanner = new Scanner(System.in);

        System.out.println("Enter first integer: ");
        int numOne;
        numOne = myScanner.nextInt();
        System.out.println("You have keyed in " + numOne);

        System.out.println("Enter second integer: ");
        int numTwo;
        numTwo = myScanner.nextInt();
        System.out.println("You have keyed in " + numTwo);

        Random generator = new Random();
        //int num = generator.nextInt(numOne) + numTwo;
        System.out.println("Random number: " + numOne + generator.nextInt(numTwo - numOne));
    }
}

after i execute this program.. i do not get the random number that's between the 2 integers.

this is my output:

Enter first integer: 
20
You have keyed in 20
Enter second integer: 
30
You have keyed in 30
Random number: 204

hope you guys can help me in any way you can (:

Preeyah
  • 363
  • 3
  • 16
  • 42
  • random.nextInt(max - min + 1) + min – Darshan Lila Apr 08 '15 at 13:17
  • Actually you get a number between 0 and 10. But you try to add the `numOne` in a wrong way. It is just being concatenated to the result string. So you see `20` and `4` but you would like `24` I guess. So you should do that addition of the numbers within parentheses. – maba Apr 08 '15 at 13:25
  • @maba i'm not trying to do addition. i'm trying to read in two integers. and then generate the random integer between those 2 integers from input/console. like.. if i type in 20 and 30.. i should get a random number between 20 and 30.. – Preeyah Apr 08 '15 at 13:51
  • Check your last line: `System.out.println("Random number: " + numOne + generator.nextInt(numTwo - numOne));`. Change that to `System.out.println("Random number: " + (numOne + generator.nextInt(numTwo - numOne)));` instead. – maba Apr 08 '15 at 13:52

2 Answers2

0

Make sure that first is always the smaller value and use new Random().nextInt(second - first) + first;. first is in the range of possible generated numbers, second is exclusive.

  • i got error - error: `incompatible types: int cannot be converted to Random`. i edited this part - `Random generator = new Random().nextInt(numTwo - numOne)+numOne;` `System.out.println("Random number: " + generator);` – Preeyah Apr 08 '15 at 13:46
  • the error is pointed to `Random generator = new Random().nextInt(numTwo - numOne)+numOne;` – Preeyah Apr 08 '15 at 13:47
  • that was just supposed to be a demonstration how to generate random numbers matching your criteria, not a solution you can simply copy paste. sry for the misunderstanding. –  Apr 08 '15 at 13:54
0

A random generator (Method in java.lang.Math?) will generate a random double x, where 0 <= x <= 1. Just multiply x with the range and add any offset to the product.

Joder
  • 9
  • 1