Harmonyos应用实例216:独立事件与互斥事件
·
8. 独立事件与互斥事件
功能简介:通过可视化展示事件的独立性和互斥性,计算相关概率。支持设置事件概率,模拟多次实验,展示事件关系的直观理解,帮助学生区分独立事件和互斥事件的概念。
ArkTS代码:
@Entry
@Component
struct IndependentEvents {
@State private pA: number = 0.5
@State private pB: number = 0.5
@State private trials: number = 1000
@State private result: string = ''
@State private eventType: string = ''
build() {
Column() {
Text('🔄 独立事件与互斥事件')
.fontSize(24).fontWeight(FontWeight.Bold)
.margin({ bottom: 20 })
Text('事件概率设置')
.fontSize(18).fontWeight(FontWeight.Bold)
.margin({ bottom: 10 })
Row() {
Text('P(A): ')
.width(60)
Slider({ value: this.pA, min: 0, max: 1, step: 0.01 })
.width(200)
Text(this.pA.toFixed(2))
.width(50)
}
.margin({ bottom: 10 })
Row() {
Text('P(B): ')
.width(60)
Slider({ value: this.pB, min: 0, max: 1, step: 0.01 })
.width(200)
Text(this.pB.toFixed(2))
.width(50)
}
.margin({ bottom: 20 })
Text('实验设置')
.fontSize(18).fontWeight(FontWeight.Bold)
.margin({ bottom: 10 })
Row() {
Text('实验次数: ')
.width(80)
Slider({ value: this.trials, min: 100, max: 10000, step: 100 })
.width(200)
Text(this.trials.toString())
.width(60)
}
.margin({ bottom: 20 })
Row() {
Button('独立事件模拟')
.width(120)
.onClick(() => {
this.eventType = '独立事件'
this.simulateIndependent()
})
Button('互斥事件模拟')
.width(120)
.onClick(() => {
this.eventType = '互斥事件'
this.simulateMutuallyExclusive()
})
}
.margin({ bottom: 20 })
if (this.eventType) {
Text(`${this.eventType}结果`)
.fontSize(18).fontWeight(FontWeight.Bold)
.margin({ bottom: 10 })
}
Text(this.result)
.fontSize(16).fontColor('#2196F3')
.margin({ bottom: 20 })
Text('概念解释')
.fontSize(18).fontWeight(FontWeight.Bold)
.margin({ bottom: 10 })
Text('独立事件: 事件A的发生不影响事件B的发生概率,P(AB) = P(A) × P(B)')
.fontSize(14).fontColor('#666')
.margin({ bottom: 5 })
Text('互斥事件: 事件A和事件B不能同时发生,P(A∪B) = P(A) + P(B)')
.fontSize(14).fontColor('#666')
}
.padding(20)
}
private simulateIndependent() {
let countA = 0
let countB = 0
let countAB = 0
for (let i = 0; i < this.trials; i++) {
const eventA = Math.random() < this.pA
const eventB = Math.random() < this.pB
if (eventA) countA++
if (eventB) countB++
if (eventA && eventB) countAB++
}
const pA = countA / this.trials
const pB = countB / this.trials
const pAB = countAB / this.trials
const pAxB = pA * pB
const difference = Math.abs(pAB - pAxB)
this.result = `实验次数: ${this.trials}\n` +
`P(A) = ${pA.toFixed(3)}, P(B) = ${pB.toFixed(3)}\n` +
`P(AB) = ${pAB.toFixed(3)}, P(A)×P(B) = ${pAxB.toFixed(3)}\n` +
`差值: ${difference.toFixed(4)}\n` +
`结论: ${difference < 0.05 ? '符合独立事件条件' : '不符合独立事件条件'}`
}
private simulateMutuallyExclusive() {
let countA = 0
let countB = 0
let countAB = 0
for (let i = 0; i < this.trials; i++) {
const r = Math.random()
if (r < this.pA) {
countA++
} else if (r < this.pA + this.pB && this.pA + this.pB <= 1) {
countB++
}
}
const pA = countA / this.trials
const pB = countB / this.trials
const pAorB = (countA + countB) / this.trials
const pAplusB = this.pA + this.pB
const difference = Math.abs(pAorB - pAplusB)
this.result = `实验次数: ${this.trials}\n` +
`P(A) = ${pA.toFixed(3)}, P(B) = ${pB.toFixed(3)}\n` +
`P(A∪B) = ${pAorB.toFixed(3)}, P(A)+P(B) = ${pAplusB.toFixed(3)}\n` +
`差值: ${difference.toFixed(4)}\n` +
`结论: ${difference < 0.05 ? '符合互斥事件条件' : '不符合互斥事件条件'}`
}
}
更多推荐


所有评论(0)