Don't use lazy static initializer in Random.secure()

The lazy static initializer returns null after throwing on the first use.

Change-Id: I44fbb6f8af263dc5c813ed5dff2291fb27e48b4e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/96984
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
Commit-Queue: Stephen Adams <sra@google.com>
diff --git a/pkg/dev_compiler/tool/input_sdk/patch/math_patch.dart b/pkg/dev_compiler/tool/input_sdk/patch/math_patch.dart
index 1e2c7dd..869c705 100644
--- a/pkg/dev_compiler/tool/input_sdk/patch/math_patch.dart
+++ b/pkg/dev_compiler/tool/input_sdk/patch/math_patch.dart
@@ -67,14 +67,14 @@
 
 @patch
 class Random {
-  static final _secureRandom = _JSSecureRandom();
+  static Random _secureRandom;
 
   @patch
   factory Random([int seed]) =>
       (seed == null) ? const _JSRandom() : _Random(seed);
 
   @patch
-  factory Random.secure() => _secureRandom;
+  factory Random.secure() => _secureRandom ??= _JSSecureRandom();
 }
 
 class _JSRandom implements Random {
diff --git a/sdk/lib/_internal/js_runtime/lib/math_patch.dart b/sdk/lib/_internal/js_runtime/lib/math_patch.dart
index a3d3fc9..677953e 100644
--- a/sdk/lib/_internal/js_runtime/lib/math_patch.dart
+++ b/sdk/lib/_internal/js_runtime/lib/math_patch.dart
@@ -63,14 +63,14 @@
 
 @patch
 class Random {
-  static final _secureRandom = new _JSSecureRandom();
+  static Random _secureRandom;
 
   @patch
   factory Random([int seed]) =>
       (seed == null) ? const _JSRandom() : new _Random(seed);
 
   @patch
-  factory Random.secure() => _secureRandom;
+  factory Random.secure() => _secureRandom ??= _JSSecureRandom();
 }
 
 class _JSRandom implements Random {
diff --git a/tests/lib_2/math/random_secure_unsupported_test.dart b/tests/lib_2/math/random_secure_unsupported_test.dart
new file mode 100644
index 0000000..7e8744c
--- /dev/null
+++ b/tests/lib_2/math/random_secure_unsupported_test.dart
@@ -0,0 +1,27 @@
+// Copyright (c) 2019, the Dart project authors.  Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+// Test that `Random.secure()` throws `UnsupportedError` each time it fails.
+
+import "package:expect/expect.dart";
+import 'dart:math';
+
+main() {
+  var result1 = getRandom();
+  var result2 = getRandom();
+
+  Expect.isNotNull(result1);
+  Expect.isNotNull(result2); // This fired for http://dartbug.com/36206
+
+  Expect.equals(result1 is Random, result2 is Random);
+  Expect.equals(result1 is UnsupportedError, result2 is UnsupportedError);
+}
+
+dynamic getRandom() {
+  try {
+    return Random.secure();
+  } catch (e) {
+    return e;
+  }
+}