Choose a random number from two numbers

I have two integers, let them be

int a = 35: int b = 70; 

I want to select one of them randomly at runtime and assign another variable. I.e.

 int c = a or b: 

One way that comes to my mind is to create an array with these two integers and find a random integer from 0 to 1 and use it as the index of the array to get the number.

Or randomize boolean and use it in if-else.

My question is, what is the best and most effective way to achieve this? That is, to choose a number from two previously defined integers?

+6
source share
5 answers

Is there a specific reason why you are requesting a more effective solution? If this function is not in a very dense inner loop somewhere (for example, in a ray tracer), you can try to optimize your code prematurely.

If you want to avoid the array, and if you don't like the "bloat" if-statement, you can use the triple select operator to choose between two:

 int a = 35; int b = 70; int c = random.nextBoolean() ? a : b; 

where random is an instance of java.util.Random . You can save this instance as a final static field in your class to reuse it.

If you do not require true randomness, but just want to switch between two numbers each time you call this block of code, you can leave by simply saving boolean and switching it:

 ... int c = toggle ? a : b; toggle = !toggle; 

Since I cannot comment on other answers, I would like to point out a problem with some other answers that involve creating a random integer in a larger range and making a decision based on whether the result is odd or even if it is less than or greater than the average. This is actually the same as generating a random integer from 0 to 1, except for an overly complex one. The nextInt(n) method uses the modulo operator for a randomly generated integer between -2 ^ 31 and (2 ^ 31) -1, which is essentially what you will do in the end, just with n = 2 .

If you use standard library methods such as Collections.shuffle() , you will again be overly complex because the standard library uses a random number generator in the standard library.

Note that all sentences are (so far) less efficient than my simple nextBoolean() , because they require unnecessary method calls and arithmetic.

+7
source

Another way to do this is to save the numbers in a list, shuffle and take the first element.

 ArrayList<Integer> numbers=new ArrayList<Integer>(); numbers.add(35); numbers.add(70); Collections.shuffle(numbers); numbers.get(0); 
+4
source

In my opinion, the main problem here is the entropy for two numbers, and not the use of this entropy. An indexed array or a boolean is essentially the same thing. What else can you do (and hopefully it will be more random) to get Java to give you a random number between the limits from 0 to 100. Now, if the selected random number is odd, you select int c = a . Choose b differently. I could be wrong, but choosing random numbers from 0 to 100 seems more random compared to choosing random between two numbers.

+1
source

You can simply use the secure random generator ( java.security.SecureRandom ).

 try { r = SecureRandom.getInstance("SHA1PRNG"); boolean b1 = r.nextBoolean(); if (b1) { c = a; } else { c = b; } } catch (NoSuchAlgorithmException nsae) { // Process the exception in some way or the other } 

Send this link for more information.

+1
source

Randomize an integer from 1 to 10, if it is greater than 5, then take the value b another wise go with a. As far as I know, there are no other ways to choose from integers.

0
source

All Articles