I want to produce 2 different random integers using java.util.random. then want to compare those two numbers, for example the code produces 5 and 9. then if one number is bigger than the other, use conditional operators to put the bigger number into variable var1. end result "9 is the bigger number". this is all I have starting out.
Random rand = new Random();
int n = rand.nextInt(50);
n += 1;
System.out.println(n);
do I have to create n1 and n2 or can I produce 2 random numbers from the same variable?
Not sure why do you even want to do this that way, but you can do it like this:
Random rand = new Random();
int var1 = Math.max(rand.nextInt(50), rand.nextInt(50));
System.out.println(var1 + " is the bigger number");
Related
I'm attempting to create a MasterCard number generator (shady, I know, but it's just for class I promise! :) ) The card number has to start with either a number between 51-55 (inclusive) or a number between 222100-272099 (also inclusive). Is there any way to set a variable to be either of these? This is the line of code I wrote, but it returned this error message: "error: bad operand types for binary operator '||' ".
int firstNum = randGen.nextInt(5) + 51 || randGen.nextInt(50000) + 222100;
This number will end up eventually being a string 16 numbers long that follows the Luhn formula, so if there is an easier way to do this/troubleshoot it, please let me know!
You can set a variable to either of those values, but only one at a time. You need to make a choice. You can make the choice in random using the random number generator you already have.
if (randGen.nextBoolean())
firstNum = randGen.nextInt(5) + 51;
else
firstNum = randGen.nextInt(50000) + 222100;
A clean solution would be to generate an array of the two random numbers and then choose one of them randomly as shown below:
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random randGen = new Random();
int[] nums = new int[2];
nums[0] = randGen.nextInt(5) + 51;
nums[1] = randGen.nextInt(50000) + 222100;
// Assign one of the random numbers to firstNum
int firstNum = nums[randGen.nextInt(2)];
System.out.println(firstNum);
}
}
You can use the || operator only between boolean expression. Another simple solution with a different distribution would be
int firstNum = randGen.nextInt(50005) + 51;
if (firstNum > 55){ firstNum += (222100-56);}
In this case there would be a roughly similar probabiblity to get 53 or 222111, but a higher probability to get a number between 222100 and 272099 then a number between 51 and 55.
I need help making a program in java that lets you write a number in textField and then generate that amount of random numbers from 0-9 using i = (int) (Math.random() * 10.0). For example if I would write 5 in the textField the program would generate 5 random numbers from 0-9.
Thanks
Using the new Java 8 streams API:
int n = Integer.parseInt(myTextField.getText());
int[] random = ThreadLocalRandom.current().ints(0, 10).limit(n).toArray();
uses the current thread local random (recommended over creating a new Random instance)
creates a random stream of integers in the range [0..10) -> 0..9
Stream terminates after n numbers have been generated
the stream result is collected and returned as an array
Ok since you want to use the Math.random() method try the following:
int times = 5;//how many numbers to output
for(int i = 0; i < times; i++)
{
System.out.println((int)(Math.random()*10));
//you must cast the output of Math.random() to int since it returns double values
}
I multiplied by 10 because Math.random() returns a value greater than or equal to 0.0 and less than 1.0.
int x = value entered in textfield;
int generatedRandomNumber = 0;
java.util.Random rand = new java.util.Random();
for(int i=0 ; i<x ; i++) {
generatedRandomNumber=rand.nextInt(10);//here you have your random number, do whatever you want....
}
I don't know how to make it in JAVA.
Sorry everybody. My case is with 51% probability, I have to do something. and, with 49% probability, I don't have to do anything.
I think I need to generate a random number, which will reference, express the probability.
how can I make it suitable to my case in Java? Thank you in advanced!
You can use the Random class. It has methods such as Random.nextInt where you can give it an upper bound and it will give you a random number between 0 (inclusive) and that number (exclusive). There are also other methods like Random.nextBoolean which returns 50% chance of true or false.
You can use Math.random function alternatively. The tutorial is here
Quoting javadoc.
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.
If you want to generate integer then you can use nextInt() method like this -
Random randomGenerator = new Random();
for (int i = 1; i <= 10; ++i){
int randomInt = randomGenerator.nextInt(100);
System.out.println("Generated : " + randomInt);
}
If you want double you can use nextDouble() method -
Random randomGenerator = new Random();
for (int i = 1; i <= 10; ++i){
int randomInt = randomGenerator.nextDouble(100);
System.out.println("Generated : " + randomInt);
}
And if you want to generate random between a range then you can do -
int shift=0;
int range=6;
Random ran = new Random();
int x = ran.nextInt(range) + shift;
This code will generate random number (int) upto 6 (from 0 to 5). If you want to generate random number shifting the lower limit then you can change the shif value. For example changing the shift to 2 will give you all random number greater than or equal 2.
Java How do I create a random number mod 5 ?
I need only random numbers 0-100, divisible by 5
something like RandomNumber.nextInt(100) % 5
Do this:
int randomMultipleOf5 = 5*random.nextInt(21);
21 is needed to get an integer in the range 0-20 (inclusive). When multiplied by 5 you get a number in the range 0-100 (inclusive).
You can just do:
Random r = new Random();
int randomMultipleOfFive = r.nextInt(21)*5; //generates a number between 0 and 20 inclusive then *5
How about;
int number = RandomNumber.nextInt(21) * 5;
To clarify, nextInt(21) generates a number from 0-20 making 100 a possible generated number, while nextInt(20) would only max generate 95.
int random = new Random().nextInt(21) * 5
Try this:
Random random = new Random();
for(int i=0; i<50; ++i){
System.out.println(random.nextInt(21) * 5);
}
I'm guessing this is very simple, but for some reason I am unable to figure it out. So, how do you pick a random integer out of two numbers. I want to randomly pick an integer out of 1 and 2.
Just use the standard uniform random distribution, sample it, if it's less than 0.5 choose one value, if it's greater, choose the other:
int randInt = new Random().nextDouble() < 0.5 ? 1 : 2;
Alternatively, you can use the nextInt method which takes as input a cap (exclusive in the range) on the size and then offset to account for it returning 0 (the inclusive minimum):
int randInt = new Random().nextInt(2) + 1;
use following function:
int fun(int a, int b) {
Random r = new Random();
if(r.nextInt(2)) return a;
else return b;
}
This will return a or b with uniform distribution.
That means in a very simple way: If you run this function N times, expected occurrence of 'a' and 'b' are N/2 each.