nextInt in java i don't understand how this works [duplicate] - java

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 7 years ago.
I'm new to java and was viewing a tutorial and saw nextInt, the guy said that it was supposed to give me a number between -5 and 5 but i have two problems :
(r.nextInt(5 - -5) + -5)
1)In what order does it operate
2)The explanation that i found says that it doesn't accept negatives then why is it supposed to allow me to give me -5?
Thanks a lot.

The answer is unary negative and subtration. Usually, that's written as +. Like
(r.nextInt(5 + 5) + -5)
which is
(r.nextInt(10) - 5)
I assume this is for a Random, in which case it returns [0,10). Which if you subtract 5, is [-5,5).

What your code will do is generate a random integer between 0 and the specified value, and will then subtract 5 from the generated integer.

Related

Java - Math.asin() only returns 0 [duplicate]

This question already has an answer here:
Division between integers in Java
(1 answer)
Closed 3 years ago.
I've been struggling with Math.asin() for a while.
System.out.println(Math.toDegrees(Math.asin(1 / 2)));
This very simple line of code pretty much sums up my desperation: The result should be 30°, however, I only get 0.0 as a result. Its not just these numbers, no matter which numbers I use, the result is 0.0.
My question is, is this a known bug of Java, or am I missing something?
Bonus Information:
I need asin() to calculate the angle between the centerpoints of two objects in my game.
You’re doing integer division, please add a decimal point in the end of each number like this:
System.out.println(Math.toDegrees(Math.asin(1.0/2.0)))

integer overflow in java [duplicate]

This question already has answers here:
How does Java handle integer underflows and overflows and how would you check for it?
(12 answers)
Closed 3 years ago.
I though that the following pieces of code does not compile. However after running it I got unexpected result, I don't understand how it is printing -2 ? can you explain how addition is done here?
int x = 2147483647+2147483647; // it compiles
System.out.print(x); // prints -2
any explain is welcome
Short answer: When Java integers reach their maximum value plus one, they start at their minimum again. It's like going in a circle.
It's like that due to the technical representation of integers as bits. Imagine having 3 bits available to represent a number. You could have the number 111. If you add 1 to it, you'll end up at 1000 but since you only have 3 bits available, it cuts off the first and you end up with 000 which is why you're at the minimum value again after adding 1 to the maximum.

Wrong result after multipling floats [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I have odd problem in my java code. Why does every number lower than 0.4f multiplied by 0.0025f gives me wrong results.
Correct result:
0.4f * 0.0025f = 0.001f
Wrong results:
0.399999f * 0.0025f = 9.999975E-4
instead of 0.000999998f
0.33333f * 0.0025f = 8.33325E-4
instead of 0.000833325f
0.11111f * 0.0025f = 2.77775E-4
instead of 0.000277775f
How are you printing your results. You should maybe look at that. It sounds like you don't understand exponential notation.
9.999975E-4 == 0.000999975
The E-4 just means shift the decimal 4 places to the right.
Furthermore, you're doing your own math wrong. You have a number ending in a 9 multiplied by a number ending in 5, which means the answer is going to end in a 5 (9 x 5 is 45, after all). So it's NOT going to be 0.000999998. You got that answer from something that rounded it, perhaps a calculator that won't show it all the way out.
You don't have a math problem. You have a display problem, and not really. It's that you don't understand the display.
Perhaps look up the printf methods and use a format string with lots of room for data after the decimal.

How to specify ranges to java.util.Random? [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 6 years ago.
I don't know why Java's designers didn't design java.util.Random objects to return random numbers like this:
rand.nextInt(minValue, maxValue);
I don't understand how to pass min and max values to this function. I've seen this recommended:
random.nextInt(max + 1 - (min)) + min
but what does that mean? Does that mean:
random.nextInt(FULL_RANGE + 1) + START_OF_RANGE
For example, if I wanted to generate values between -1 and 1, inclusive, is this how I do it:
int oneOfThreeChoices = rand.nextInt(1 + 1 + 1) - 1;
The API provides sufficient tools for you to complete this task. If you want an random int on the range [a,b], inclusive on both ends, use
int n = random.nextInt(b-a+1) + a
assuming that random is an object previously declared to be of type java.util.Random.
random.nextInt(bound) returns random number from 0 to bound (exclusive). For example random.nextInt(10) returns random number from 0 to 9.
So, if you want to return number from 5 to 15 (inclusive) just say
random.nextInt(11) + 5
Why java designers have not included more convenient API to JDK? I think because the way explained here is pretty simple and obvious.

Java math random inclusive 1 [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Write a truly inclusive random method for javascript
(10 answers)
Closed 9 years ago.
What is the easiest way to include 1 using Math.random function?
Since the default is number between inclusive 1 and exclusive 0.
I am going to use my the rand function a lot of times in my code that is why i need an even probability of getting numbers between 0 and 1 [0,1] , inclusive 0 and inclusive 1.
Thanks!
Edit:
Sorry, big mistake. Exclusive 1, inclusive 0.

Categories