diff --git a/core/modules/system/src/Tests/Entity/EntityUnitTestBase.php b/core/modules/system/src/Tests/Entity/EntityUnitTestBase.php
index a11bce1deb185587e2225d986086b0f30eaf536b..89deb62437d7394e90449322fa861f5fbb867e00 100644
--- a/core/modules/system/src/Tests/Entity/EntityUnitTestBase.php
+++ b/core/modules/system/src/Tests/Entity/EntityUnitTestBase.php
@@ -188,7 +188,10 @@ protected function refreshServices() {
   protected function generateRandomEntityId($string = FALSE) {
     srand(time());
     do {
-      $id = $string ? $this->randomMachineName() : mt_rand(1, 0xFFFFFFFF);
+      // 0x7FFFFFFF is the maximum allowed value for integers that works for all
+      // Drupal supported databases and is known to work for other databases
+      // like SQL Server 2014 and Oracle 10 too.
+      $id = $string ? $this->randomMachineName() : mt_rand(1, 0x7FFFFFFF);
     }
     while (isset($this->generatedIds[$id]));
     $this->generatedIds[$id] = $id;