2

I'm currently working on a side project in Unity while in school majoring in Game Development. I'm running into an issue with the seed I use for map generation. I can choose to turn random seed generation on or off, and if off, I can input a seed for the map to be generated from. However, the problem I'm running into is that when I turn off random generation and input a seed, the user inputted seed, despite being identical to a randomly generated one, produces a different map than the randomly generated seed.

For example, say my random seed generator script spits out the string "hollow tasty boardwalk". I get the hashcode of that string and slap it into System.Random(). However, if I turn off random generation and type in "hollow tasty boardwalk" manually, the code still just takes the hashcode and puts it into System.Random(), but the map generated is completely different.

Here is the code used to seed the random generator:

public string seed;
public bool useRandomSeed;
public SeedGenerator gen;

if (useRandomSeed)
{
    gen.GenerateSeed();
    seed = gen.seed;
}

System.Random pseudoRandom = new System.Random(seed.GetHashCode());

If useRandomSeed is false, I just give it a value in the editor, since it's public. So like I mentioned earlier, let's just say for example my GenerateSeed() method sets gen.seed to "prickly swift hatbox". That in turn sets the seed in my MapGenerator script here to "prickly swift hatbox" and uses that to see the random number generator. However, if I manually set the seed to "prickly swift hatbox" from the Unity editor (or, as I plan to do in the future, a text window within the game), the map generated is completely different.

I suspect it has something to do with GetHashCode, as I'm probably misunderstanding how it works (I'm still pretty new to this). If that's the case, I'm not really sure of a simple workaround to turn my seed string into an int that the random generator can use. Can anyone help me confirm this issue and/or point me to some resources for a workaround? I'm pretty adamant about using a word string for the seed since it's a lot more user friendly that a lump of numbers. Thank you in advance!

Edit: The problem actually appears to be within my SeedGenerator script. So basically, it generates a list of words from a text file with \n separating entries. Either in the text file or in the code that creates the phrases, an invisible character exists that effectively makes the randomly generated seeds completely different than the user-inputted ones, since the user can't see/type that invisible character. Now I just have to find out where that character is and how to stop it!

Edit 2: The problem is in my adjective text file itself (the problem doesn't occur with nouns). Each word in the text file is separated by an "enter" key press, and when the file is read, it is split with "\n". For some reason, this leaves in an extra, invisible character at the end of each word. However, in my noun text file, everything appears to be separated by "shift+enter", and they have no invisible character afterwards.

Now, here's where things get interesting. Copy/pasting a list of "shift+enter" spaced words into a new text file changes the "shift+enter"s to plain "enter"s, causing the invisible character to appear. This is where I initially/unintentionally caused this error, as my adjectives are copy/pasted from various lists of common adjectives I found across Google. Using String.Trim in a foreach loop after reading from the file and slapping the words into an array of strings doesn't cut off the invisible character, so that's a no go. Copy/pasting the invisible character, interestingly, turns it into a normal "space". I have no clue why. So far, the only solution I can come up with is manually going into my adjective file and re-spacing all of the ~400 entries with "shift+enter".

Edit 3: My messy but functioning fix was to manually go through my adjectives list and replace every return with a space, and have my script split the file with ' ' instead of '\n'.

  • 2
    Well I'd start by logging the hash code you're passing into `Random`, so you can work out whether the problem is there or with `Random`. Bear in mind that different versions of .NET and the like can have different implementations of `GetHashCode` - there's no guarantee that it'll be the same in two separate runs for the same string even in the same environment. (IIRC, a debug version of regular .NET has some sort of fuzzing precisely to *stop* people depending on this.) You may want to look into something like MD5 instead... a repeatable hash. – Jon Skeet Mar 01 '17 at 21:51
  • 1
    I've found the problem! When my Seed Generator script reads the nouns and adjectives from a text file, it includes a new line or some invisible character in between words, so that when I retype the seed and those characters aren't included, it's actually a different seed. I have no idea what the character is and how/why it got in there, but this is good progress :) – Hunter Jones Mar 01 '17 at 22:19
  • Interesting, perhaps you could use [`String.Trim()`](https://msdn.microsoft.com/en-us/library/t97s7bs3(v=vs.110).aspx) to try to sanitize whatever individual words you're reading from your text file? – Serlite Mar 01 '17 at 22:37
  • @HunterJones - So, the problem here, with this question, is that you haven't shown us enough code to solve your problem. What you should do is post the minimal amount of code required to demonstrate your issue. Had you done that you may have even figured out your issue on your own, but, if not, we would have been able to help you. You should read [mcve]. – Enigmativity Mar 01 '17 at 22:38
  • Okay, I discovered the problem exactly I think. My nouns were not giving me a problem, and I had found that text file online through some Google search. My adjectives, however, which I had made myself by copy/pasting lists of adjectives from various websites, had the invisible character immediately at the end of them and before the \n. It turns out that a normal "enter" key press causes this invisible character to appear, while "shift+enter" doesn't. Additionally, copy/pasting a list of terms separated by a "shift+enter" turns them into normal "enter"s. How inconvenient. – Hunter Jones Mar 01 '17 at 22:41
  • That invisible character is probably a [\r](http://stackoverflow.com/questions/15433188/r-n-r-n-what-is-the-difference-between-them) – Draco18s no longer trusts SE Apr 27 '17 at 19:47

0 Answers0