String hash random number seed

Does anyone know a good way in C# to convert a string into a random number seed for random generation? Basically similar to the random seed entered in Minecraft just before level generation so that the level is generated exactly the same with the same seed, but made so that it can use a string instead of a number. (I assume it would hash the string into a number that can be then used as a seed?)

The simplest form is

string.GetHashCode();

1 Like

Oh okay, that was a lot simpler than I expected.

And then to use it as a random number seed, you basically do a new Random() with the hash code?

It returns an int, so it’s down to you what you do with it, but it can be used for anything you like, you can use it as is as the seed.

If two string objects are equal, the GetHashCode method returns identical values. However, there is not a unique hash code value for each unique string value. Different strings can return the same hash code.

So you may want to do something different for uniqueness. If someone else don’t beat me, i will see what i can come up with.

I’m not too worried about the possibility of a repeat showing up with two different strings if the number of possible hashcode ints is large enough, it probably won’t matter for the purposes of the project.