3

Hi i am new to java and stackoverflow. I am trying to create a program that asks for the user to input a range and it will create a random number list within the users range. It looks like this.

package numbergenerator;

import java.util.Random;
import java.util.Scanner;

public class numbergenerator {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        Random r = new Random();

        int int1;
        int int2;
        int answer;
        int numbers;


        System.out.println("Enter your min range number: ");
        int1 = s.nextInt();

        System.out.println("Enter your max range number: ");
        int2 = s.nextInt();

        System.out.println("How many numbers do ya want? ");
        numbers = s.nextInt();

        for(int counter=0; counter<numbers; counter++);
        answer = int1 < r > int2;
        //this doesn't work. 
        //And i know it is improper syntax, but then what is the proper syntax?
        System.out.println(answer);

    }

}

I am having problems with how to get the random number. Let me know if you have an idea. Also i might have the for loop messed up as well, so just let me know. thanks.

2
  • Calculate the range (max - min), calculate a random number from 0-range, add min Dec 12, 2014 at 5:35
  • You also have a bug with for(int counter=0; counter<numbers; counter++); - no semicolon needed Dec 12, 2014 at 5:40

4 Answers 4

1

Calculate the range of numbers you need...

// Make sure we only have positive numbers...not sure if that's a requirement
// bet lets make life easier
int int1 = Math.abs(int1);
int int2 = Math.abs(int2);
int range = Math.abs(int2 - int1); 

Calculate your random number within that range...

int answer = r.nextInt(range);

Add your lower range to it...

answer += Math.min(int1, int2);

nb: You should know, that this will only generate random numbers between min to max - 1. If you want to include max, you will need to add 1 to the range...

5
  • I don't know why someone downvoted this. It's a great solution. Dec 12, 2014 at 5:42
  • I "think" maybe because it will only generate numbers in the range of min-(max - 1), but that requirement hasn't been stated... Dec 12, 2014 at 5:44
  • 1
    You could also use Math.abs(int2 - int1) if you want to ensure that the range is positive and don't trust the user to follow the instructions :)
    – jbeck
    Dec 12, 2014 at 5:45
  • @jbeck True, but you should also be using Math.min(...) - Math.max(...) ;) - One would question if that was outside the scope of the question though ;) Dec 12, 2014 at 5:46
  • @jbeck Ah, no wait, I think I see what you mean :P Dec 12, 2014 at 5:50
0

try this

  package numbergenerator;

    import java.util.Random;
    import java.util.Scanner;

    public class numbergenerator {
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            Random r = new Random();

            int int1;
            int int2;
            int answer;
            int numbers;


            System.out.println("Enter your min range number: ");
            int1 = s.nextInt();

            System.out.println("Enter your max range number: ");
            int2 = s.nextInt();

            System.out.println("How many numbers do ya want? ");
            numbers = s.nextInt();

            for(int counter=0; counter<numbers; counter++){
            answer =r.nextInt(int2-int1)+int1;

            System.out.println(answer);
}

        }

    }
2
  • The 'answer = ...' and 'System.out ...' lines need to be in the scope of the for loop (ie curly braces as in the OP). As written, the for loop has no effect on the output of the program and only one answer would be generated and displayed.
    – jbeck
    Dec 12, 2014 at 5:57
  • this will never generate a number equal to the upper limit (int2) modify to answer =r.nextInt(int2-int1 +1)+int1; so you get the correct range
    – MihaiC
    Dec 12, 2014 at 6:59
0

Say you have the following values:

int max;
int min;

If you want to get a new random number between max and min, you need to understand how the Random class works.

nextInt(int n) returns a number that is greater than or equal than zero, and less than(but not equal to) n.

Therefore, if you want to get a number between max and min, inclusive of both, you would have to do the following:

Random r = new Random();
int randomNumber=r.nextInt(max-min+1)+min;
0
0
package numbergenerator;

import java.util.Random;
import java.util.Scanner;

public class numbergenerator {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        Random rn = new Random();

        int int1;
        int int2;
        int answer;
        int numbers;


        System.out.println("Enter your min range number: ");
        int1 = s.nextInt();

        System.out.println("Enter your max range number: ");
        int2 = s.nextInt();

        System.out.println("How many numbers do ya want? ");
        numbers = s.nextInt();

        for(int counter=0; counter<numbers; counter++);
        {
              int n = int1 - int2 + 1;
              int i = rn.nextInt() % n;
              randomNum =  int1 + i;
              System.out.println(randomNum );
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.