I'm trying to use Apache's HashCodeBuilder in an android app. They give the following example code in the documentation:
public class Person {
String name;
int age;
boolean smoker;
...
public int hashCode() {
// you pick a hard-coded, randomly chosen, non-zero, odd number
// ideally different for each class
return new HashCodeBuilder(17, 37).
append(name).
append(age).
append(smoker).
toHashCode();
}
}
My question is, how random do these numbers actually need to be? They state that the numbers should ideally be different for each class, so I was thinking of declaring a hashcodeSeeds.xml android resource file, where I can store all of the values for each class in one place, to ensure that they are all different for each class. My plan was to do just increment by odd numbers, like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="timeOfDay_hash_seed_1">1</integer>
<integer name="timeOfDay_hash_seed_1">3</integer>
<integer name="calendarEvent_hash_seed_1">5</integer>
<integer name="calendarEvent_hash_seed_2">7</integer>
...
</resources>
Is incrementing through each odd number like this (e.g. 1, 3, 5, 7) random enough? In other words, by "random" do they really just mean arbitrary? Or do I need to use an RNG?
Aucun commentaire:
Enregistrer un commentaire