Are there any java.util.random seeds with bad/weird properties? [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I'm curious if there are seeds for java.util.random which have weird/surprising properties. This has little practical use, but I'm still curious.
edit: by weird/surprising properties I mean repeating values or atypical patterns.

Yes!
The java.util.Random object's constructor takes in a long as an argument. A bit of sifting through internal classes, we find that this long is processed through the following function:
private static long initialScramble(long seed) {
return (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1);
}
The goal of this function is to scramble the seed enough so the resulting seed is uniformly distributed across the long range (or something like that).
All we have to do is find an input to this function where it will produce an output of 0 to break things 😊
A bit of brute forcing and I was able to find the long -9223372011639871891
, Using this long as an input, the function returns 0!
Now, we can define a random like the following:
var random = new Random(-9223372011639871891L);
The first call to random.nextInt() will always be zero, no matter the range!
Initial calls to other random functions (nextDouble, nextFloat, etc.) will also produce VERY low values (but not exactly zero)
Of course, this all only applies to the first call to the "next" functions, but still cool regardless!

Related

New thread per each boolean true combination [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a doubt about what would be the better way to achieve this:
I have a Class that contains 5 boolean variables (example: 11111, being one combination). Now, the thing is that I'm gonna start a new thread per each true combination, meaning that if i have 11000 i want to start a thread for 11000, another for 10000 and the last one for 01000.
I have to know all the values of the input combinations. The most obvious and inefficient way would be just comparing all the 32 (in this case) combinations (00000, 00001, etc.) and starting only when and AND of that and the actual value (11000) is != 0, that would be the cases that i previous mentioned (11000, 10000 and 01000).
In that case I'm gonna have to do 32 comparisons every single time. The thing is that if then i have 6 booleans now i have to do 64, and so on.
Anyone can think of a better strategy to "capture" every combination?
I think the only way to do it faster is through pre-calculation.
So, String[][] arr; and for (String a : arr[5]) {/* thread creation etc */} should be used.
Where '5' it is '00101'.
Also might helps: Integer.toBinaryString(5) and (int) Long.parseLong("00101", 2).

How to check whether all valid values entered to my program will be correctly calculated? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I created a same sized - stack based simple calculator for adding 2 operands. I want to know whether my addition becomes incorrect when specific (valid) values are entered. The 2 stacks take integer values and are having the same number of digits (i.e. 400 [3 digits] and 900 [3 digits]).
It depends on the algorithm you're using. From the question it is not clear, but let's assume your calculator can perform basic arithmetic. First off, you want to test each operation separately, because they have different equivalence classes of their inputs. For example, for multiplication it would be: 0, 1, minimum and maximum values, and their negations. Testing almost always will not be exhaustive, but using equivalence classes, you can pick one value from each class to make sure that each class is covered with a test.
Back to your question, you may use min/max values, and anything that you think may break your code.

Optimization of java code for reducing complexity [closed]

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 5 years ago.
Improve this question
I am actually new to java libraries. I dont know what this code is doing.
I have been given a task to optimize this. Any help will be useful.
I mean to ask whether str.equals(local) checks for entire string aur for particular index value in the for loop.
public void fnc(String str, int[] ar1)
{
String local = "findnumber";
for(int i=0; i<ar1.length; i++)
{
if(str.equals(local) && ar1[i] * 2 > 10)
{
Integer ip = new Integer(ar1[i]);
ip = ip * 2;
System.out.print(ip.toString());
}
}
}
The big O complexity of the algorithm is O(N) where N is the array size. You cannot improve on that ... for the arguments provided and producing the same output.
There are some things that can be done to improve efficiency though.
Hint: look for a computation that is performed on each loop iteration that could be performed once.
Hint: look for some unnecessary object creation1
Hint: unnecessary use of a reference type.
There are one or two other questionable micro-optimizations, but see if you can spot them without any hints. (I say "questionable" because I suspect that the JIT compiler would do the same optimization itself.)
One final note: the actual speed of the code will be dominated by the print statement, and its ability of the OS to write stuff to (for example) the console. And probably by JVM startup / warmup effects ... unless the fnc method is called many times.
1 - Notwithstanding anything else, new Integer(...) is the wrong way to convert an int to an Integer.

How can I go through several possibilities without exhausting loops? [closed]

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 7 years ago.
Improve this question
How can I go through several possibilities without exhausting loops in a program that sum of the proper divisors(divisors including 1 but not itself) of the number is greater than the number, but no subset of those divisors sums to the number itself.
For Example:
12=(factors sum)1+2+3+4+6
Atleast Any Subset Sum should be equal to the number i.e 12 in this case.
Question link
:http://www.practice.geeksforgeeks.org/problem-page.php?pid=301
Approach:
Step1 (Doubt)
By looking at the problem I found that its optimal solution will be Dynamic Programming.(Since I don't know that).
Apart from that I thought of the solution like this
For Example 12=1,2,3,4,6(divisors)
So,Solving it through like this with 1 starting having all permutations: 1+2+3+4+6 or 1+3+4+6 or 1+4+6 or 1+6(checking at each step whether it is <=12.
Similarly,I checked starting with 2: 2+3+4+6 or 2+4+6(I got and moved out of the loop)
The solution I am thinking here is very long.I have to put seperate loop for each number and also I am saving the divisors in a string.
Can anyone give me "Hint" how to start the problem without the Dynamic Programming(Dp) approach as I am learning Dp these days.
I assume you have a method
int[] getDivisors(int number)
the hard task here is to evaluate the possible sums.
An easy but long way would be, to iterate over all of those like that.
Think of such a sum as a binary code, 1 if its in sum, 0 if its not.
Now you can write a for-loop which, from 0...0 to 1...1 (in binary) with #1s = divisors.length;
Maybe a better way would be recursion methods. Like:
boolean hasSameSum(int number, Stack<Integer> divisors, int sum){
if(sum==number)
return false;
if(divisors.isEmpty())
return true;
int div = divisor.pop();
return hasSameSum(number, divisors, sum)&&hasSameSum(number, divisors, sum+div);
}
But honestly, that's not a programming problem, but more a mathematical approach problem :-)

using the group Z4 to shorten code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
is it possible to have a variable set in the group of Z4. So that if i had the following code
"int" x = 3;
x = x + 1;
the value of x would be 0. I know I can use mod 4 but I was wondering if you can change "int" to make it mod 4
I know I can use mod 4 but I was wondering if you can change "int" to make it mod 4.
It is not possible in Java.
(In fact, I cannot think of any mainstream programming language where you can do this. In Ada and one or two other languages you can define types that are subranges of an integer type, but I don't recall one where you could define a range type with modular arithmetic.)
Here are some alternatives:
Define a class with static helper methods that perform arithmetic on Z4 values stored as int.
Define a class whose values represent Z4 values with a range of arithmetic methods.
Just do the arithmetic using regular int and integer operators, and convert to Z4 at the appropriate time by masking out all but the 2 bottom bits.

Categories