diff --git a/core/modules/simpletest/src/TestBase.php b/core/modules/simpletest/src/TestBase.php
index e57048f..39c2df8 100644
--- a/core/modules/simpletest/src/TestBase.php
+++ b/core/modules/simpletest/src/TestBase.php
@@ -1298,22 +1298,32 @@ protected function settingsSet($name, $value) {
   }
 
   /**
-   * Generates a unique random string of ASCII characters of codes 32 to 126.
+   * Generates a unique pseudo-random string of ASCII characters of codes 32 to 126.
    *
    * Do not use this method when special characters are not possible (e.g., in
    * machine or file names that have already been validated); instead, use
    * \Drupal\simpletest\TestBase::randomMachineName().
+   * For avoiding random test failures, special characters will be ensured.
    *
    * @param int $length
    *   Length of random string to generate.
    *
    * @return string
-   *   Randomly generated unique string.
+   *   Pseudo-randomly generated unique string including special characters.
    *
    * @see \Drupal\Component\Utility\Random::string()
    */
   public function randomString($length = 8) {
-    return $this->getRandomGenerator()->string($length, TRUE, array($this, 'randomStringValidate'));
+    // To prevent random test failures, ensure that the returned string contains
+    // a character that needs to be escaped in HTML by splitting the length in
+    // half and injecting an ampersand into it.
+    $first = floor($length / 2);
+    $last = $length - $first - 1;
+    $parts = array('', '&', '');
+    foreach (array(0 => $first, 2 => $last) as $index => $length) {
+      $parts[$index] = $this->getRandomGenerator()->string($length, TRUE, array($this, 'randomStringValidate'));
+    }
+    return implode('', $parts);
   }
 
   /**
