Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pick a number randomly from two numbers

Tags:

java

random

I have two integers, let them be

int a = 35:
int b = 70;

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

int c = a or b:

One of the ways that come into my mind is to create an array with these two integers and find a random integer between 0 and 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 that is there a better and more efficient way to achieve this? I.e. pick a number from two previously defined integers?

like image 918
Basant Singh Avatar asked Mar 17 '14 04:03

Basant Singh


People also ask

How do you pick a random number between two numbers?

The Excel RANDBETWEEN function returns a random integer between given numbers. RANDBETWEEN is a volatile function recalculates when a worksheet is opened or changed. This formula is then copied down from B5 to B11. The result is random numbers between 1-100.

How do you randomly pick a number from a range?

Use randint() Generate random integer Use a random. randint() function to get a random integer number from the inclusive range. For example, random. randint(0, 10) will return a random number from [0, 1, 2, 3, 4, 5, 6, 7, 8 ,9, 10].

How do I generate a random number between two numbers in Excel?

Select the cell in which you want to get the random numbers. In the active cell, enter =RANDBETWEEN(1,100). Hold the Control key and Press Enter.

What is a random two digit number?

Therefore, the 10 distinct random two-digit numbers will be 52, 76, 90, 37, 63, 12, 74, 21, 54, 11 .


1 Answers

Is there a specific reason you are asking for a more efficient solution? Unless this functionality sits in a very tight inner loop somewhere (e.g. in a ray tracer), you might be trying to prematurely optimize your code.

If you would like to avoid the array, and if you don't like the "bloat" of an if-statement, you can use the ternary choice operator to pick between the two:

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

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

If you don't require true randomness, but just want to switch between the two numbers in each invocation of the given block of code, you can get away with just storing a boolean and toggling it:

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

Since I can't comment on other answers, I'd like to point out an issue with some of the other answers that suggest generating a random integer in a bigger range and making a decision based on whether the result is odd or even, or if it's lower or greater than the middle value. This is in effect the exact same thing as generating a random integer between 0 and 1, except overly complicated. The nextInt(n) method uses the modulo operator on a randomly generated integer between -2^31 and (2^31)-1, which is essentially what you will be doing in the end anyway, just with n = 2.

If you are using the standard library methods like Collections.shuffle(), you will again be overcomplicating things, because the standard library uses the random number generator of the standard library.

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

like image 164
Andreas Troelsen Avatar answered Oct 15 '22 00:10

Andreas Troelsen