I'm pulling my hair out with this and I thought I'd see if the greater Java experience of others might be able to shed some light on the problem. There is a large amount of program code I have written, which is itself within a larger project so I can't simply post it. However, I will outline the problem...
The issue: My code does not return predicatble results from a simulation. Each time the simulation is run, various statistics are recorded. Despite the fact that the code is identical (!), the results produced differ between executions.
Things I am pretty sure are not a problem (although if you think there's a "gotcha" please shout out):
Can anyone think of something I might be overlooking here? This seems unfathomable at the moment! Thanks!
Are you sure that you are using the instance of Random that you created in all places? Remember that calls to Math.random() use their own instance of Random.
I'm assuming you've overridden both equals()
and hashCode()
in your objects (or you've written a custom comparator). If your random number is used in your objects, it's not a criteria in the sort, right?
Also, if you're sorting these items then placing them into a HashMap or a HashSet - these structures will not preserve your order - use TreeSet or TreeMap instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With