diff --git a/core/modules/simpletest/src/TestBase.php b/core/modules/simpletest/src/TestBase.php
index e57048f..a0600a2 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 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($first, $last) as $index => $length) {
+      $parts[$index] = $this->getRandomGenerator()->string($length, TRUE, array($this, 'randomStringValidate'));
+    }
+    return implode('&', $parts);
   }
 
   /**
diff --git a/core/modules/simpletest/tests/src/TestBaseTest.php b/core/modules/simpletest/tests/src/TestBaseTest.php
index 50e08b5..e002187 100644
--- a/core/modules/simpletest/tests/src/TestBaseTest.php
+++ b/core/modules/simpletest/tests/src/TestBaseTest.php
@@ -63,4 +63,17 @@ public function testRandomStringValidate($string, $expected) {
     $this->assertEquals($expected, $actual);
   }
 
+  /**
+   * Tests the random strings contains a non-alphanumeric char.
+   *
+   * @see \Drupal\simpletest\TestBase::randomString().
+   *
+   * @covers ::randomString
+   */
+  public function testRandomString() {
+    $string = $this->stub->randomString(8);
+    $this->assertEquals(8, strlen($string));
+    $this->assertTrue(strpos($string, '&') > 0);
+  }
+
 }
