diff --git a/contrib/tsm_system_rows/expected/tsm_system_rows.out b/contrib/tsm_system_rows/expected/tsm_system_rows.out
index 7e0f72b..4b8bcaa 100644
--- a/contrib/tsm_system_rows/expected/tsm_system_rows.out
+++ b/contrib/tsm_system_rows/expected/tsm_system_rows.out
@@ -11,14 +11,14 @@ SELECT count(*) FROM test_tablesample TABLESAMPLE system_rows (1000);
 SELECT id FROM test_tablesample TABLESAMPLE system_rows (8) REPEATABLE (5432);
  id 
 ----
+ 10
+ 17
+ 24
+  0
   7
  14
  21
  28
-  4
- 11
- 18
- 25
 (8 rows)
 
 EXPLAIN SELECT id FROM test_tablesample TABLESAMPLE system_rows (20) REPEATABLE (10);
diff --git a/contrib/tsm_system_time/expected/tsm_system_time.out b/contrib/tsm_system_time/expected/tsm_system_time.out
index 32ad03c..e07860a 100644
--- a/contrib/tsm_system_time/expected/tsm_system_time.out
+++ b/contrib/tsm_system_time/expected/tsm_system_time.out
@@ -11,6 +11,10 @@ SELECT count(*) FROM test_tablesample TABLESAMPLE system_time (1000);
 SELECT id FROM test_tablesample TABLESAMPLE system_time (1000) REPEATABLE (5432);
  id 
 ----
+ 10
+ 17
+ 24
+  0
   7
  14
  21
@@ -38,10 +42,6 @@ SELECT id FROM test_tablesample TABLESAMPLE system_time (1000) REPEATABLE (5432)
  20
  27
   3
- 10
- 17
- 24
-  0
 (31 rows)
 
 EXPLAIN SELECT id FROM test_tablesample TABLESAMPLE system_time (100) REPEATABLE (10);
diff --git a/src/backend/access/tablesample/bernoulli.c b/src/backend/access/tablesample/bernoulli.c
index 0a53900..3fafcb4 100644
--- a/src/backend/access/tablesample/bernoulli.c
+++ b/src/backend/access/tablesample/bernoulli.c
@@ -146,7 +146,7 @@ tsm_bernoulli_nexttuple(PG_FUNCTION_ARGS)
 	 *
 	 * (This is our implementation of bernoulli trial)
 	 */
-	while (sampler_random_fract(sampler->randstate) > probability)
+	while (1.0 - sampler_random_fract(sampler->randstate) > probability)
 	{
 		tupoffset++;
 
diff --git a/src/backend/utils/misc/sampling.c b/src/backend/utils/misc/sampling.c
index aaf1d6c..42e3147 100644
--- a/src/backend/utils/misc/sampling.c
+++ b/src/backend/utils/misc/sampling.c
@@ -94,7 +94,7 @@ BlockSampler_Next(BlockSampler bs)
 	 * less than k, which means that we cannot fail to select enough blocks.
 	 *----------
 	 */
-	V = sampler_random_fract(bs->randstate);
+	V = 1.0 - sampler_random_fract(bs->randstate);
 	p = 1.0 - (double) k / (double) K;
 	while (V < p)
 	{
@@ -233,11 +233,11 @@ sampler_random_init_state(long seed, SamplerRandomState randstate)
 	randstate[2] = (unsigned short) (seed >> 16);
 }
 
-/* Select a random value R uniformly distributed in (0 - 1) */
+/* Select a random value R uniformly distributed in (0.0,1.0] */
 double
 sampler_random_fract(SamplerRandomState randstate)
 {
-	return pg_erand48(randstate);
+	return 1.0 - pg_erand48(randstate);
 }
 
 
