This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 8 years ago.
using iOS I sometimes like to call 2 functions at random a button press for example, I would use; So sometimes, the user would get (1) the other time (2) etc.
if (arc4random() % 2 == 0) {
// Do one thing (1)
} else {
// Do something else(2)
}
}
How would I do this within Eclipse/java? In otherwords, what is the same statement but in a Java language?
Use the Java Random class. This would give you either 1 or 2:
Random rand = new Random();
int n = rand.nextInt(2) + 1;
nextInt(n) gives you a random number from 0 to n-1 (inclusive). So you have to add 1 to the result.
This question already has answers here:
Returning a random even number
(5 answers)
Closed 9 years ago.
How do you generate random numbers that go up by twos using Math.random()? For example, I'm trying to generate a random number from the set (2,4,6,8), how would you go it?
For this specific set you could use
(int)(Math.random() * 4) * 2 + 2
Here:
Math.random() generates a number that's greater or equal to 0.0 and strictly less than 1.0;
(int)(... * 4) gives one of 0, 1, 2, 3.
... * 2 + 2 gives one of 2, 4, 6, 8.
Okay, let's make a real general solution.
int lower = 2;
int upper = 8;
int step = 2;
int rand = (int)(Math.random() * (upper-lower+1));
int result = rand - rand%step + lower;
If you want to generate numbers within another set than the one you specified, just change the lower, upper and step variables to fit your set. It includes the upper bound if it's in the set.
The (almost) completely general approach isn't that hard.
static randInt(int first, int last, int step) {
int nsteps = (last+1-first) / step;
return first + step*(int)(nsteps*Math.random());
}
That returns a random integer from {start, start+step, start+2*step, ... } up to and including stop, or the last number before stop if stop is not part of the sequence.
int choice = randInt(2, 8, 2); /* random choice from {2, 4, 6, 8} */
...solved the sample question.
The (almost) part is that this doesn't handle integer overflow or sign errors in the arguments (step is zero, step<0 when first<last, or vice versa.)
int[] numSet={2,4,6,8};
Random myRand=new Random();
int myNum=numSet[myRand.nextInt(numSet.length)];
This creates an array of numbers, the random object, then picks a random element from the array of numbers. This does not assume that the numbers are in any sort of mathematical series. They could be 11, 25, 31, 876, 984368, 7432, 84562, for that matter.
Please don't tell me that there's a polynomial that generates that set from its y-values. I know.
Put those numbers in an array.
Generate a random number using the length of the array - 1 and use it as the index to randomly choose an array element.
You can also use the java.util.Random class like this:
int values[] = {2,4,6,8};
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(values.length);
int randomValue = values[randomInt]; /* Here's your random value from your set */
I would like to add a more general answer.
Assume the smallest integer in the set is A and the size of the set is S. A random integer from the set would be like this:
A + myRandom.nextInt(S) * 2
For your specific example in Java, it would be:
Random myRandom = new Random();
int randomNum = 2 + myRandom.nextInt(4) * 2;
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 9 years ago.
So I am trying to generate a random number, but I can't use the Java random function because I need the numbers to be in the range of 1-25. What is the easiest aka most efficient way of doing this? If possible, an explanation would be great!
int random = (int)(Math.random() * 25 + 1);
or
int random = new Random.nextInt(24) + 1;
I prefer to use the Java Random Class.
import java.util.Random;
And then generate the random nuber like this - assuming you want an integer:
Random gen = new Random();
int r = gen.nextInt(25) + 1;
Random numbers between 1 and 25 (1-25)
Random.nextInt(24) + 1
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Generating random number in a range with Java
I am trying to create a random number that is above the value of one boundary, and below another. The number can be equal to either of the boundaries too.
Both boundaries are created from random. highNumber is a random between 0 and 100, and lowNumber is a random between 0 and half of highNumber.
At the moment my code is as follows:
public static void createCorrectNumber() {
random = new Random();
correctNumber = random.nextInt(highNumber)+1;
correctNumber -= lowNumber;
}
This is not functional, as when the lower bound is taken away from it, it can become lower than the boundary. Any ideas?
use
correctNumber = random.nextInt(highNumber - lowNumber + 1) + lowNumber;
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Java: generating random number in a range
I need a little help.
What code would I use to create a random number that is 5 digits long and starts with either 1 or 2?
In order to use as a company employees ID?
Depending on how you approach the problem something like that:
public int gen() {
Random r = new Random( System.currentTimeMillis() );
return 10000 + r.nextInt(20000);
}
Or something like that (you probably want the instantation of the Random object of of the method but I just put it here for simplicity) :
public int gen() {
Random r = new Random( System.currentTimeMillis() );
return ((1 + r.nextInt(2)) * 10000 + r.nextInt(10000));
}
The idea is that 1 + nextInt(2) shall always give 1 or 2. You then multiply it by 10000 to satisfy your requirement and then add a number between [0..9999].
Here's are some example output:
14499
12713
14192
13381
14501
24695
18802
25942
21558
26100
29350
23976
29045
16170
23200
23098
20465
23284
16035
18628