Java Random Number with seed changes after a day [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I was using the code below to generate random number with seed so that I can generate back the same results next time.
int seed = 100;
Random rand = new Random(seed);
I manage to get back the same results from the program on the same day, but after a day or two i get a completely different results. Is there a problem of the way I implement the random seed? Anyone encounter this before?

Citing from Javadoc:
If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers.
It really depends on how you use that instance. It has to be strictly the same sequence of methods you call.
This means the problem is in the code that calls the methods on your Random instance. The sequence of the methods called seems to depend on some conditional that can change it.

Related

Are there any java.util.random seeds with bad/weird properties? [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 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!

1e5 which is equivalent to 10^5 . Is this value represents double or int in java. Can You please elaborate the concept [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 months ago.
Improve this question
This Code is giving error that lossy conversion from double to int.
int[] ans=new int[1e5+1];
The concept is simple: 1E5 is a 'double' floating-point number in Java because the Java language specification says it is.
Meanwhile, an array size must be integral, and the compiler is telling you that. If you really want to use a floating-point number there, you need to cast to int. However, it's easier to just write 100000.

Random.nextInt seems wrong from a book [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
It's a block of code for random verification code in a Servlet book.
the underlined part must be a number of 30+?
but the intention is one of those chars above
Pic
NextInt method only has one parameter which is the max number (exclusive)?
So is it a mistake in the book?
nextInt(int) returns a [pseudo]random number between 0 and the parameter passed to it, exclusive. Thus, if you pass a length of an array, you'd get a random valid index from the array, which you can then use to pick a random element from it, as this code does.

Can someone explain what's going on here step-by-step? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
public int pray(int secondsPrayed){
int randomRecoveryValue = secondsPrayed + new Random().nextInt(3);
int actuallyRecoveredValue = Math.min(this.MAX_MP -this.mp,randomRecoveryValue);
this.mp += actuallyRecoveredValue;
return actuallyRecoveredValue;
}
This is from a book I'm currently using to learn. The objective is to create a pray method for a Priest object that restores its MP by the amount of seconds prayed plus 0 to 2 at random. I don't quite understand two things:
3 after nextInt rather than 2 (the book's answer) - why is this when we're trying to get 0 to 2 at random?
actuallyRecoveredValue returns its value to secondsPrayed; however, to me it looks like that to get actuallyRecoveredValue in the first place we need RandomRecoveryValue, which in turn needs secondsPrayed, which at that point doesn't seem to exist? I'm new to Java so I don't have much experience with returning values and such.
nextInt(int n)
Returns a pseudorandom, uniformly distributed int value between 0
(inclusive) and the specified value (exclusive), drawn from this
random number generator's sequence.
You pass secondsPrayed as variable into the method, so it does exist inside the method (scope). From the code you posted, we can't tell if actuallyRecoveredValue returns its value to secondsPrayed is true.
to me it looks like that to get actuallyRecoveredValue in the first
place we need RandomRecoveryValue, which in turn needs secondsPrayed
it's true, but as stated above, secondsPrayed does exist
1) The nextInt() method returns an integer value between zero and the parameter you used (3 in your case). While zero is included in the range, the last element of the range is not.
You can think that nextInt(n) returns n possible values, meaning the range between "0" and "n-1"
nextInt(5) // can return 0,1,2,3,4
2) The code is a little bit messy, I'll give you that, and as you said to get you have to determine the value of "randomRecoveryValue" in order to determine actuallyRecoveredValue. The method pray() does three things:
Calculates the recovered value, based on the input param and some randomness
Adjusts this value, so that the priest cannot heal over its full health. That happens at the third line, basically the priest heals for a number of points equal to "randomRecoveryValue" only if its current MP is not too high, otherwise he heals for the value that will get him to full health.
Update the value of his MP
Returns how much the he actually healed, not necessary, but may be useful to the caller of this method
Cheers

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 :-)

Categories