nextFloat is not working on Random object [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
why this line of code has error message ?
Random ran2 = new Random();
float distance = ran2.nextFloat(50f);

The method nextFloat(float) is implemented in interface java.util.random.RandomGenerator (docs.oracle.com), which was introduced in Java 17 (en.wikipedia.org).
Compiling this code with a JDK < 17 will result in a compilation error:
...
... error: method nextFloat in class Random cannot be applied to given types;
...
There are two ways to solve this issue:
Upgrade to a JDK >= 17 (the recommended approach)
Copy what RandomGenerator.nextFloat(float) does (the actual implementation can be found in jdk.internal.util.random.RandomSupport (github.com)):
float distance = ran2.nextFloat();
distance = distance * 50f;
if (distance >= 50f) // may need to codistancedistanceect a distanceounding pdistanceoblem
distance = Float.intBitsToFloat(Float.floatToIntBits(50f) - 1);

Related

When does Common subexpression elimination apply? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
for (Pair p : pairs) {
double f = foo(p)
...
}
foo() performs a simple mathematical calculation as follows:
double foo(Pair p) {
return Math.cos(p.x) + Math.sin(p.y);
}
If all the items in pairs are the same, can the Java compiler optimise this using Common subexpression elimination? Or is there another optimisation that occurs? I am asking since I have found a significant time reduction when more values in pairs are the same.

Java - Gamma algorithm [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am trying to code an algorithm to implement Gamma Correction but I am unable todo so with below code, because of a high power base. Wondering if anyone could fix below code to raise color values between 0,1 Thanks
The problem is in your math.
int / int will cause a truncation.
If red = 9 and you execute 9 / 255 the result is 0.
Try making all your literal values floating point
(for example 255.0 instead of 255).

python big integer multiplication doesn't calculate most significant digits [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a python program which should multiply 2 big integers.
The problem is I get a different result in java with BigInteger class with same input.
I have tried DecInt library for python but gives the same answer as using pure python.
Here are my variables:
d = 372049305848826709205673800090501485720867768816
r = 5452188953055713107393819158892374332916586527736541041226026450382
Result I get in python from d * r:
2028483115341019294875069650745272851135156323450218238187883716036516369477015140871224045070868977706272670887712
Result I get in java with BigInteger class:
9530687378863294988874153740700860249994095546182028483115341019294875069650745272851135156323450218238187883716036516369477015140871224045070868977706272670887712
Here is my java code:
BigInteger d = new BigInteger("372049305848826709205673800090501485720867768816");
BigInteger r = new BigInteger("5452188953055713107393819158892374332916586527736541041226026450382");
BigInteger tmp1 = d.multiply(r);
System.out.println(tmp1);
As you can see, there are some most significant digits that are missed in python's result.
Is there any solution for that?
Both Java and Python give the same answer:
2028483115341019294875069650745272851135156323450218238187883716036516369477015140871224045070868977706272670887712
The problem must be in your Java code then. Make sure you preform multiplication correctly and verify your input.
Also, check if there is anything that might print digits just before the result. The simplest way I can think of is:
System.out.println("the result is: " + int1.multiply(int2));
That will separate the output you are interested in from everything that is already printed.

Value assigning from text file variables [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Got a problem and I don't know how to approach this one.
From the text file I get a line like this: "x = 45 + 3". (Variables are only single lowercase/uppercase letters). Output should be 48, like normal calculation.
Now I need to calculate that equation and use the 'x' in another equation like
"y = x + 15 - 7".
So far I've come up an idea to use substring to get the calculation part, i.e '45+3'. But I can't think any good idea how to keep variable and use it in another equation since next equation is a string form a text file also.
Any ideas are welcome!
use replace all x with 45 do the technique of arithmetic you had done before for 45+3
Hope my help works.
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
engine.eval("x = 45 + 3");
engine.eval("y = x + 15 - 7");
double y = (Double)engine.get("y");
System.out.println(y);

Digital (number) length in Java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How to get the length of some number in Java?
Length of string is string.length(), but what is it for an integer?
I tried this:
int lengthNumber = (String.valueOf(maxNumber)).length();
But it always returns 1, for every number.
Try
Integer#toString().length();
For Example
Integer a=222;
int length=a.toString().length();
Output
3
When I ran this:
int Number = 100003;
int lengthNumber = (String.valueOf(Number)).length();
System.out.println(lengthNumber);
My output was 6, indicating that it works correctly.
Just make sure that your variables are declared properly.
This method will work if the above isn't working.
int x = 100003;
String y = "" + x;
Now you can use y.length(). Printing y gives 100003, and printing y.length() gives 6.
System.out.println(y);
System.out.println(y.length());

Categories