Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I just have a simple question. In Java, we know that the remainder of (y%x) can't be greater than x itself. Thus, we could hypothetically set all numbers less than a certain value of x, say 100. Yet what would be the opposite of this? What if we wanted to set all numbers above a certain value, say 20, to have a range [20,100]?
I was thinking that we could subtract 20 from both sides to have the range [0,80], and then take the modulus of 80, and then add 20 to it.
Am I right in believing this?
Thanks.
The Random class includes nextInt(int) which (per the Javadoc) Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. So, 0-80 is
Random rand = new Random();
int v = rand.nextInt(81);
If you want it in the ragne 20-100 that would be
int v = rand.nextInt(81) + 20;
How can the remainder be greater that the divisor ? In other words you division is not complete.
If you want to of from 20-80 you can simple do
int i = 20 + randomInteger % 80;
This will be in the range of 20 -100.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have to check, roomNumber[i], if BOTH numbers (assuming it's two-digit) are not even, and not odd. Only then can I accept the number. Also need to determine if the second digit is not less than double the first. I assume that I need to some how split these into two different ints (a,b), but I'm not sure.
If you assume that you have only two-digit numbers, you can go with it this way:
int[] roomNumbers = new int[] { 12, 34, 56, 67, 78 };
for (int num : roomNumbers) {
int second = num % 10;
int first = (num - second) / 10;
System.out.println("first: " + first + " second: " + second);
}
You take the second number using the modulo (%) operator. To get the first number, you need to subtract the second number and divide it by 10.
Now you can process those numbers further.
Your questions have nothing to do with arrays. Let's reword them:
How do I test if a number is even?
You can google this and easily find the answer. Hint: use the modulus operator (%).
How do I test if the second digit is not less than double the first digit
Break this into smaller pieces: First you need to separate out the first and second digit. Again, you can google this if you don't know. (Hint: it's very similar to the first question. You can use the % operator and division (/).
Then you have to compare these according to the rule. Try to write something on your own and see what you can come up with.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Some teacher today teach us how work Math.random in Java language. I don't understand what he told. Can some explain? I have some code:
import java.util.Random;
public class test {
public static void main(String[] args) {
Random random = new Random();
double r = Math.random() * 4.4 + 1.2;
System.out.println(r);
}
}
Next he ask us what is interval of this random. We don't know and he write:
<1.2, 5.6> --> <0, 4.4> + 1.2 --> double r = Math.random() * 4.4 + 1.2;
How can I calculate this, what is mathematical formula? <1.2, 5.6> --> <0, 4.4> + 1.2
Math.random() gives a random number between 0 and 1. Actually it's a pseudo random but that's a different story. So in order to have a random number for example between 0 and 100 you need to multiply Math.random() by 100.
That makes you range from 0..1 to 0..100 because 0x100=0 and 1x100=100.
By adding a number to the result you set the lower boundary of the range. For example if you want a number between 100 and 200 you can do:
Math.random()*100 + 100.
So in your case he multiplies it by 4.4 giving it a range 0->4.4 and then adds 1.2 giving it a lower boundary of 1.2 which makes the actual range 1.2->5.6
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Hello I an not a java programmer,
I need know what this function does:
rpcToken = Math.round(1E8 * Double.parseDouble("0." + (int) (Math.random() * Integer.MAX_VALUE)));
This just generate a random 8 number string? I need to make this exactly in PHP.
Thank you
Math.random() gives you a pseudorandom number less than 1 but at least 0.
This is multiplied by Integer.MAX_VALUE, which is 2147483647. The result is a number between 0 and 2147483646.
(int) converts this number to an integer.
"0." + converts this to a String, and adds "0." to the beginning of it.
Double.parseDouble converts this String back to a double. So now we again have a number between 0 and 1. But any number greater than 0 but less than 0.1 is impossible due to the way we created the String. "0.9" and "0.900" would both convert to 0.9.
1E8 * multiplies this by 1E8, which is 100000000.
And finally, Math.round rounds this off to the nearest integer.
So, in total, we have a supposedly random number somewhere between 0 and 99999999, but the final result is very skewed. Many numbers are impossible to generate with this code (numbers between 1 and 9999999 seem impossible, but 0 is still possible) and this code will generate certain numbers more often than other numbers (close to half the generated numbers will begin with 1, and more than average may end with 0), and I find it very unlikely that all of this is intentional (although it's impossible to say for sure without knowing the code's purpose.)
If you just need a random 8 digit number, there are easier, faster, and better ways. Why not just multiply that random number you got in the first step by 100000000, and round it off?
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I need to find the number of total possible different solution to an equation with more than one variable.
For example:
1x + 2y + 8z = 13
What is the total number of different combinations for the values of x, y and z?
I can't think of an algorithm to solve this. I don't need the answers printed, just the total number of different combinations. The coefficients and variables will always be positive, and the final number too.
1x + 2y + 8z = 13
Hence try (x, y,z) from (0, 0, 0) upto (13, 6, 1)
Hence at most 14*7*2 tries
Sort by the highest coefficient: z, y, x. The last variable can be inferred
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am having trouble generating this code which I'm sure I'm just in a coder's block or something because it seems as though it should be easy but can't get it for the life of me.
I have a program which needs random numbers generated within a certain range which is to represent money. The stipulation is that the money need be in between 2 and 200 and represent only even dollars only so $2, $4, $6...so on. I have done extensive searching online which yield a bounty of code to represent random numbers in a range in java but not the part about being only even.
Any ideas?
If you wanted to be clever, you could make sure the least significant bit of the number is not set:
int num = (new Random().nextInt(199) & ~1) + 2;
This will ensure that the number is always even.
Thanks Eyal Shneider and Omaha
Marcelo's comment from the OP is the correct answer, though.
Get a number between 1-100, and multiple by 2:
int num = (new Random().nextInt(100) + 1) * 2;
int rand = new Random.nextInt(200);
int result = 2;
while(result < rand) {
result += 2;
}
return result;
(I'd make it recursive but I've got to go meet my wife for dinner.)