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);
}
Related
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 am looking to get random number in between 1000 to 8192000. The random number should be like 1000 , 2000,3000 to 8192000.
Following is the code that i have tried but did not got any success.
ran.nextInt(8192000 - 1000)%1000;
What should I change in order to get number in term of 1000, 2000, 3000...
The easiest approach would seem to generate a random number between 1 and 8192 and just multiply it by 1000:
Random randomGenerator = new Random();
long randomNumber = (1 + randomGenerator.nextInt(8192)) * 1000L;
If you want 8192000 inclusive try:
Random random = new Random();
for (int i = 0; i < 10; i++) {
System.out.println((random.nextInt(8192) + 1) * 1000);
}
Here you get values: 1000, 2000, ..., 8192000
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 4 years ago.
Consider the following code:
int rand = new Random().nextInt((30 - 20) + 1) + 20
It will return a random number between 30 and 20. However, I need its range to include negative numbers. How would I include negative numbers in the generation?
I have tried using math that would be negative, but that resulted in an error. Simply subtracting or adding the negative numbers would not yield the desired value.
Sorry, I am only half awake. The correct code is int rand = new Random().nextInt((30 - 20) + 1) + 20;.
To get a random number between a set range with min and max:
int number = random.nextInt(max - min) + min;
It also works with negative numbers.
So:
random.nextInt(30 + 10) - 10;
// max = 30; min = -10;
Will yield a random int between -10 and 30 (exclusive).
It also works with doubles.
You can use Random.nextBoolean() to determine if it's a random positive or negative integer.
int MAX = 30;
Random random = new Random(); // Optionally, you can specify a seed, e.g. timestamp.
int rand = random.nextInt(MAX) * (random .nextBoolean() ? -1 : 1);
Okay. First, try to only create the Random instance once, but for an example,
int rand = -15 + new Random().nextInt(31);
is the range -15 to 15.
The Random.nextInt(int) JavaDoc says (in part) Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive). Note that your provided example of (30 - 20) + 1 is the range 0 to 10 (inclusive).
As a further example, to get the range 20 to 30 you would use:
int rand = 20 + new Random().nextInt(11);
Remember, the bounds of the result of is 0 to n.
30 - -10. I'm trying to make a survival simulator. That will be my temperature.
Ok. Let's write that range as -10 to 30. nextInt(n) will return a value between 0 and n, so if you want the range below 0 you must subtract 10 from the result and add 10 to the n. That's
Random random = new Random();
int rand = random.nextInt(41) - 10;
Now let's examine how we can determine those numbers. Remember, nextInt() will return between 0 and n (exclusive) and we want -10 to 30 (inclusive); so 41 is n and we subtract 10. If the result is 0 (the min) we get -10, if the result is 40 (the max) we get 30.
int randomNumber=(random.nextInt(DOUBLE_MAX)-MAX);
Alternatively, create a random positive or negative 1 and multiply it.
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 9 years ago.
how would I create up to 15-20 random numbers between 100-200 in java?
I have this atm but it creates any random numbers but I want the numbers to be between 100 and 200 but I don't know how I would go about adding this to the code below. please can someone help.
Random rand = new Random();
int Randnum;
for(int i = 0; i <=20; i++) {
System.out.println(Randnum + " ");
}
}
This has been answered before, but use rand.nextInt(int n). This will generate a number between 0 (inclusive) and n (exclusive). In your case, use rand.nextInt(101)+100 to generate a number between (and including) 100 and 200.
Random rand = new Random();
int Randnum;
for(int i = 0; i <=20; i++) {
Randnum = rand.nextInt(101)+100;
System.out.println(Randnum + " ");
}
}
Random rand = new Random();
int Randnum;
for (int i = 0; i <= 20; i++) {
Randnum =rand.nextInt(101) + 100;
System.out.println(Randnum + " ");
}
nextInt(n) method of Random class returns a number between 0(inclusive) and n(exclusive).
In your case, you need a number between 100 and 200, so fetch a number using nextInt with values ranging from 0 to 101 (you get numbers from 0 to 100) and add 100 to it to get numbers from 100 to 200.
You can either use Random or Math#random
use Math.random()
You can do something like:
int[] randnum = new int[20];
for(int i = 0; i <20; i++)
{
randnum[i] = (int)((Math.random() * 101)+100) ;
}
now you have 20 integers between 100 and 200.
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 9 years ago.
I am trying to create a random phone number with a range. The format being (xxx)-xxx-xxx and the area code not starting with 0,8, or 9 and the next set of three being in a range from 100-742 and then the last set of 4 can be any digit. How would i create the first two parts? Any help would be appreciated. Thanks!
import java.util.*;
import java.text.*;
public class PhoneNumber{
public static void main(String[] arg){
Random ranNum = new Random();
//int areaCode = 0;
//int secSet = 0;
//int lastSet = 0;
DecimalFormat areaCode = new DecimalFormat("(000)");
DecimalFormat secSet = new DecimalFormat("-000");
DecimalFormat lastSet = new DecimalFormat("-0000");
//DecimalFormat phoneNumber = new DecimalFormat("(###)-###-####");
int i = 0;
//areaCode = (ranNum.nextInt()); //cant start with 0,8,9
//secSet = (ranNum.nextInt()); // not greater than 742 and less than 100
//lastSet = (ranNum.nextInt(999)) + 1; // can be any digits
i = ranNum.nextInt();
System.out.print(areaCode.format(i));
i = ranNum.nextInt();
System.out.print(secSet.format(i));
i = ranNum.nextInt();
System.out.print(lastSet.format(i));
}
}
well, basically, you need to generate numbers in two ranges
[1; 7]
[100; 742]
To have random integer in range [m; n] you could write:
updated (remove Math.random())
int numberInRange = m + new Random().nextInt(n - m + 1);
HTH
1,2,3,4,5,6,7
makes 7 different values
ranNum.nextInt(7)+1; //So 1 is your lowest number and 7 is the number of different solutions
nexInt will range between 0 and intPassed exclusive,
So ranNum.nextInt(7) will run between 0 and 6, + 1 makes 1 .. 7
This will range between 1 and 7
You can take the same principal for the second range
You can try the next:
int sec = java.util.concurrent.ThreadLocalRandom.current().nextInt(100, 743);
And so on with the other parts of the phone number.
This method returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).
Just make a random number greater than 99 and less than 800, then the next would be about the same way.
Random rand = new Random();
// add 1 to make it inclusive
max min
int firstRandomSet = rand.nextInt((799 - 100) + 1) + 100;
//none starts with 0,8, or 9
int secondRandomSet = rand.nextInt((742 - 100) + 1) + 100;
//produces anything from 100-742
to get the numbers 0001 - 9999 you'll have to be creative.
int maxValues= 9999;
int thirdRandomSet = rand.nextInt(maxValues);
System.out.printf("%04d\n", thirdRandomSet);