-1

I would like to regenerate a random Int. I generate 3 Int like this:

Random a = new Random();
int testa = a.nextInt(9 - 1) + 1;
Random b = new Random();
int testb = b.nextInt(9 - 1) + 1;
Random c = new Random();
int testc = c.nextInt(9 - 1) + 1;

And if 1 of them is same, I want to regenerate the Int. How?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Fachri
  • 19
  • 6
  • 2
    So compare and re-generate... – Eugene Sh. Jan 05 '17 at 15:13
  • 2
    a simple if else statement would suffice – Bradley Wilson Jan 05 '17 at 15:14
  • 2
    See the answers below, I do not agree-- it looks simple, but a "junior" might face a challenge there... . – Peter Branforn Jan 05 '17 at 15:24
  • if I do this : http://textuploader.com/ddptc it said " Variable "testb" is already defined in the scope. I am newbie for this cooding ;) – Fachri Jan 05 '17 at 15:25
  • Why do you create three instances of `Random` class? This is usually not wanted, unless you have very specific reason and you understand the implications of such code. As you stated you are newbie, consider it bug and simplify your code to use only one Random instance. – Ped7g Jan 05 '17 at 15:26
  • And I mean "only one instance" per application runtime, not even "one per main loop"/etc, that's still too many of them... it's hard to tell what you are building from this short source. – Ped7g Jan 05 '17 at 15:32
  • Did my answer help you? – Jagruttam Panchal Jan 05 '17 at 15:38
  • @JagruttamPanchal Thanks for helping.. But I am a beginer, I cant understand your code. I'll save if I need this. – Fachri Jan 05 '17 at 15:52
  • @Fachri, Not an issue! Thanks! – Jagruttam Panchal Jan 05 '17 at 16:05
  • @Fachri, the basic idea in your own attempt is sound, better than some of the answers: first find `testa`; then find a `testb` that is different from `testa`; finally find a `testc` that is different from both of the first two. Only, since the next attempt, and the next again, may also find a number that you have already, you cannot avoid some loop. – Ole V.V. Jan 05 '17 at 20:00

5 Answers5

3

Use a Set and add random numbers inside the loop till the length reaches the length you need.

int rInt;
Random a = new Random();
Set<Integer> random = new HashSet<>();
while (random.size() < 3) {
    rInt = a.nextInt(9 - 1) + 1;
    random.add(rInt);
}

To get the random integers from the Set you can either use an Iterator or use an ArrayList as follows,

List<Integer> list = new ArrayList<>(random);
int tempa = list.get(0);
int tempb = list.get(1);
int tempc = list.get(2);

Imports required,

import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.ArrayList;

I hope it helps. :)

K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33
  • As the OP is very fresh to Java programming, maybe you should add also example how the values from Set can be used instead of `testa/b/c` local variables. – Ped7g Jan 05 '17 at 15:29
  • @Ped7g this is not a site where we explain incredibly basic concepts to beginners. This entire page should be deleted – Tim Jan 05 '17 at 15:30
  • @TimCastelijns I'm usually in favour of some newbie topics, but in this case it's probably way too much, plus several answers still promote bad practice code, so they are actually misleading... yeah, you are right. – Ped7g Jan 05 '17 at 15:33
  • If you want to help a beginner in Java, you probably shouldn’t post code with two compile errors in it. – Ole V.V. Jan 05 '17 at 19:55
  • @OleV.V. Check the edit. Didn't get time to complete the answer. – K Neeraj Lal Jan 06 '17 at 05:21
  • @Ped7g Noted. Edited the answer. – K Neeraj Lal Jan 06 '17 at 05:22
  • 1
    And having some time to think about it... this may produce unexpected ordering of those numbers, like they may be even sorted all the time, as HashSet will not keep the ordering. So you should have also another array `int[3] intArr`, and `if (random.add(rInt)) { intArr[random.size()-1] = rInt; }` This way the order of random numbers would be saved. At that point the accepted answer is actually better, except that abundant instancing of Random. So I'm taking away my upvote, as this is uselessly "high level" :) (common trap in Java). – Ped7g Jan 06 '17 at 07:20
1

You could use a while statement , do something like this:

Random a = new Random();
int testa = a.nextInt(9 - 1) + 1;
Random b = new Random();
int testb = b.nextInt(9 - 1) + 1;
Random c = new Random();
int testc = c.nextInt(9 - 1) + 1;

while(testa != testb && testa != testc && testb != testc){
  Random a = new Random();
  testa = a.nextInt(9 - 1) + 1;
  Random b = new Random();
  testb = b.nextInt(9 - 1) + 1;
  Random c = new Random();
  testc = c.nextInt(9 - 1) + 1;
}

This just should keep making new random integers until they are 3 different ones.

Kaushal28
  • 5,377
  • 5
  • 41
  • 72
  • 1
    why to use this random generator code two times? Use do while instead. – Kaushal28 Jan 05 '17 at 15:23
  • 2
    And you also win the price for the highest number of Random class instances created per second. (this is not positive) – Ped7g Jan 05 '17 at 15:30
1

You can generate list of numbers first and then shuffle. Then peek one by one from that shuffled list.

See my answer here: How to randomly pick element from array without repeating?

Hope will help you!

Community
  • 1
  • 1
Jagruttam Panchal
  • 3,152
  • 2
  • 15
  • 21
  • When you want three random numbers of eight possible values, I clearly recommend this answer. No explicit `Random` instances, no risk of many loop iterations, can even be done with no explicit loop at all. Also no `Set` to interfere with the order you get your numbers. – Ole V.V. Jan 06 '17 at 21:52
0

You should keep in mind that this is not really "random" anymore! So do not use it for security related features such as generating a random three-digit number!

Set<Integer> myRandoms = new Set<Integer>();
while(myRandoms.size() < 3)
{
// generate random value
  myRandoms.add(randValue);
}

Why does this work? Because a set can only grow if each entry in it is unique (different from each other).

Mistalis
  • 17,793
  • 13
  • 73
  • 97
Peter Branforn
  • 1,679
  • 3
  • 17
  • 29
  • 2
    And why doesn’t it work? (1) Because `Set` is an interface, you cannot do `new Set()`. (2) Because most `Set` implementations (including `HashSet` and `TreeSet`) have a deterministic order they returns their elements, so while the numbers are random, their order is not. – Ole V.V. Jan 05 '17 at 15:42
-1

You can simply do something like this without using any Set or lists:

do{

   //your code 
     Random a = new Random();
     int testa = a.nextInt(9 - 1) + 1;
     Random b = new Random();
     int testb = b.nextInt(9 - 1) + 1;
     Random c = new Random();
     int testc = c.nextInt(9 - 1) + 1;

}
while(testa == testb || testb == testc || testa == testc);
Kaushal28
  • 5,377
  • 5
  • 41
  • 72