2

I'm new to programming java and i need to make random integers between 2 values. I could not find any relevant info in the oracle tutorials.

because i'm new to java, most of what i researched i don't understand. I have looked at the following:

http://docs.oracle.com/javase/7/docs/api/java/util/Random.html

Generating a Random Number between 1 and 10 Java

http://www.javapractices.com/topic/TopicAction.do?Id=62

How do I generate random integers within a specific range in Java?

Getting random numbers in Java

i have tried:

import java.util.Random;

public static void main(String[] args) {
    Random randint = new Random();
    System.out.println(randint);

result:

java.util.Random@52e922

it appears to be printing the type of randint.

so, how do i create a random integer between 2 values, and then print the result to the screen?

Community
  • 1
  • 1
Thedudxo
  • 549
  • 3
  • 8
  • 22
  • what is that 2 values you want? – Baby Nov 25 '14 at 01:13
  • @TheQuickBrownFox any, does not matter. i want to apply this to multiple cases. – Thedudxo Nov 25 '14 at 01:14
  • You looked at the documentation--did you see `nextInt()`? The one with the bound? You will have to use that and then add something to it. – ajb Nov 25 '14 at 01:14
  • http://stackoverflow.com/questions/5887709/getting-random-numbers-in-java – BatScream Nov 25 '14 at 01:14
  • possible duplicate of [Generating a Random Number between 1 and 10 Java](http://stackoverflow.com/questions/20389890/generating-a-random-number-between-1-and-10-java) – TLP Nov 25 '14 at 01:49
  • @TLP in case you did not notice, i have seen that question. it's not a dupe. – Thedudxo Nov 25 '14 at 03:17
  • @Thedudxo Well, yes, it is. The question and accepted answer is exactly the same. How do you figure its not? – TLP Nov 25 '14 at 03:32
  • @TLP i actually used a combination of the accepted answer and your answer. i did not understand the usage of the random class as well as not understanding that the random class did not have any max/min by default. – Thedudxo Nov 25 '14 at 05:31

5 Answers5

4

If you want a value between two integer values (min and max) Here you go:

Random r = new Random();
int randInt = r.nextInt(max-min) + min;
System.out.println(randInt);

So max would be 5 and min would be 2 if you want a integer between 2 and 5

EDToaster
  • 3,160
  • 3
  • 16
  • 25
1

You can specify the range of the random number like this:

Random random = new Random();
int min = 2;
int max = 5;
int x = random.nextInt((max-min)+1) + min;

The number that will be generated will be between 2 and 5.

If you do not need to specify any range then you can do it like this:

Random random = new Random();
int x = random.nextInt(11);

Note that the random number in this example will be between 0 and 10.

In either case, You will need to include the following import statement to your program as long as you want to use the Random class.

import java.util.Random;
Bu Saeed
  • 1,173
  • 1
  • 16
  • 27
0

You need to make a int or something to store them in

Random randint = new Random();
int a = randint.nextInt(2)+1;
System.out.print(a);

Do note however I use +1 because java always counts from 0. So this will produce 1 or 2.

0

You are printing the Random object. You need to use a function of the object to get a random integer:

Random random = new Random();
int i = random.nextInt(2);
System.out.println("Random value: " + i);
TLP
  • 66,756
  • 10
  • 92
  • 149
  • this works, but only from 0 to 2. how do i do it from say, 2 to 5, or something like that? – Thedudxo Nov 25 '14 at 01:18
  • You know, that is written in the answer to the question you linked to in your answer: `random.nextInt(max - min + 1) + min`. See for yourself here: http://stackoverflow.com/a/20389923/725418 – TLP Nov 25 '14 at 01:19
0

It functions to me, the possible results includes the max value but not the min.

int random = (int)(Math.random() * (max - min) + 1) + min
beginner
  • 21
  • 3