Random seed a String ?

Looking at the java Random class it supports a long as a seed
minecraft for example has a string seed

without writing it yourself obviously, what would I use if I want a string as a seed ?

edit: or to ask differently: how is this done usually ?
like every character casted to int, and then, added or whatever ?

I think I remember him saying that he just takes the toByteArray() and does something with it. Or even just .hashCode() ?


String strSeed = "yessir";
long seed = 0;
for (int i=0;i<strSeed.length();i++)
{
      seed+=(int)strSeed.charAt(i);
}	
Random random = new Random(seed);

of course this works, but many collisions, as “yessir” and “siryes” will obviously yield the same result (not really sure if thats a problem in my case)

hashCode() huh ?

Yeah: just using the default hashCode result as the seed is good enough for gaming purposes.

well hashcode is the hashcode of the OBJECT

so it seems “blah”.hashCode() always returns the same thing - if thats true, well then good, although I dont get it
because


"blah".hashCode();
"blah".hashCode();

2 lines of code, 2 strings, 2 different objects, right ? They just happen to have the same string value.

That’s because String overrides hashCode() to return the hash of the String.

alright then

Apart from the fact that you actually have one object there due to string interning, if you expect “blah”.equals(“blah”) to return true then either you expect “blah”.hashCode() == “blah”.hashCode() to return true or you expect HashMap to break badly when strings are used as keys.

as pjt33 said when you do it like this you have only one object, because string constants get cached.
try new String(“blah”)