Skip to content

Instantly share code, notes, and snippets.

@liyuqian
Created October 29, 2020 03:04
Show Gist options
  • Save liyuqian/3ad9c2a2b06c771df53a1db0407f7df1 to your computer and use it in GitHub Desktop.
Save liyuqian/3ad9c2a2b06c771df53a1db0407f7df1 to your computer and use it in GitHub Desktop.
Simulate the expected number of days to have all devices failed randomly.
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