-1

How do I print a number that is not 'x'. But within the given range.

int x = (int) (Math.random() *3);// I have generated a random number
System.out.println(x);// This will print the random number

There is some confusion understanding this problem so i will explain it with the help of an example.

Suppose 'x' is equal to 2

Now i want to print a number that is not equal to 'x' for example 1, which is within the range stated above.

sg7610
  • 293
  • 2
  • 8
  • For an integer range, repeat getting another random number until it isn't equal to `x`. (But then your "random sequence" will not be random any more!!) – laune Mar 21 '16 at 19:14
  • Possible duplicate of [Getting random numbers in Java](http://stackoverflow.com/questions/5887709/getting-random-numbers-in-java) – Vighanesh Gursale Mar 21 '16 at 19:14
  • What you are saying is to generate a new random number and it will not be equal to 'x'. That's not the solution and chances are that i will get the same number. What i want is to print a number that is not 'x' and within that given range i.e '0-2'. – sg7610 Mar 21 '16 at 19:16
  • Sorry - my previous comment was saying that the basic random int sequence does not repeat the same number. But mapping into some smaller integer interval may of course hit the same number again. See my next comment! – laune Mar 21 '16 at 19:19
  • Depending on the property of the random algorithm, a sequence of numbers obtained from r1, r2, r2,... by mapping using some modulus operation and discarding repeats may even be a cycle over the (small) set of integers in your range. If the range has a length of (say) 3, the cycle may have a length of 6, or 12 - the sequence will not be random. - Perhaps you don't want to do this repeatedly, but then, what's the point of your question anyway? – laune Mar 21 '16 at 19:24
  • If you just need a *pair* of different random numbers from within a range, e.g. `a <= 3`, `b <= 3`, `a != b`: then calculate a, and repeat calculating b until not equal to a. The next pair may have an `a' ` equal to b. – laune Mar 21 '16 at 19:28
  • @laune If you make that an answer, I'll upvote it. – Paul Boddington Mar 21 '16 at 19:37
  • @PaulBoddington Your proposal is just as good - but I still don't see whether OP wants those *pairs* - or what? – laune Mar 21 '16 at 19:47

4 Answers4

0

Try the Random class

Random r = new Random();
int max = 10;
int min = 1;
int x = r.nextInt((max - min) + 1) + min;// I have generated a random number
System.out.println(x);// This will print the random number
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

I would do:

int possibilities = 7;
int x = ThreadLocalRandom.current().nextInt(possibilities);
int y = ThreadLocalRandom.current().nextInt(possibilities - 1);
if (y >= x)
    y++;     // adding 1 to y here means that y can't equal x

This way y is equally likely to be any of the integers in the range 0 to possibilities - 1 except x.

ThreadLocalRandom.current().nextInt(possibilities); is just an alternative way of generating a random number in a range. ThreadLocalRandom.current() just gives an instance of the Random class, and nextInt is a method for producing random numbers in a range (I prefer this to the Math.random approach).

However, you may find it easier to understand this solution, which is more similar to your original question.

int possibilities = 7;
int x = (int) (Math.random() * possibilities);
int y = (int) (Math.random() * (possibilities - 1));
if (y >= x)
    y++;
Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
  • Thank you. This really worked, as i am very new to java it is hard for me to understand what you wrote. It will be a huge help if you can explain it to me in brief. – sg7610 Mar 21 '16 at 19:46
  • I think using == is more appropriate in `if (y >= x)` – sg7610 Mar 21 '16 at 20:02
  • @SarthakGaur No, because then the value x + 1 is twice as likely to get picked (you could either pick x + 1 in the first place, or pick x in the first place and add 1). The `y >= x` condition is a neat way of ensuring all values apart from x are equally likely. – Paul Boddington Mar 21 '16 at 20:03
  • Conditions: y > x : No problem, as they are not equal. y = x : Use if, make it greater than x by adding one. Thus, condition 1. y < x : No problem, as they are not equal. Chances of y being greater than x is 2/3 Is this what you are saying if I use the == operator. – sg7610 Mar 21 '16 at 20:18
  • @SarthakGaur I'm not sure I understand your approach. If you want an answer that obviously works use laune's suggestion in the comments. – Paul Boddington Mar 21 '16 at 20:27
0

I assume you want to print a random number within gives range in java you replace your line

int x = (int) (Math.random() *3);

with this code

 Random rand;
 int x = rand.nextInt((max - min) + 1) + min;
Denis
  • 1,219
  • 1
  • 10
  • 15
0

Given your code:

int x = (int) (Math.random() *3);
int y = (int) (Math.random() *3);
if(y != x){
   System.out.println(x);
}
TheMirrox
  • 368
  • 2
  • 13