Created
October 29, 2020 03:04
-
-
Save liyuqian/3ad9c2a2b06c771df53a1db0407f7df1 to your computer and use it in GitHub Desktop.
Simulate the expected number of days to have all devices failed randomly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
void main() { | |
const int deviceCount = 5; | |
const double failRate = 0.5; | |
const int sampleCount = 1000; | |
final random = Random(); | |
int sum = 0; | |
for (int i = 0; i < sampleCount; i += 1) { | |
final Set<int> failedDevice = {}; | |
int t = 0; | |
for (; failedDevice.length < deviceCount; t += 1) { | |
for (int d = 0; d < deviceCount; d += 1) { | |
if (random.nextDouble() <= failRate) { | |
failedDevice.add(d); | |
} | |
} | |
} | |
sum += t; | |
} | |
print(sum / sampleCount); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment