我想在矩阵(2x2)中生成随机数,以产生特定范围的结果。特别是结果“行列式”必须是以下之一:
(1 或 3 或 5 或 7 或 9 或 11 或 13 或 15 或 17 或 19 或 21 或 23 或 25)
并遵守这个等式:
(RandomA x RandomY)-(RandomB x RandomZ)=one of the aforementioned Range of the "Determinant"
(从左到右):
A 是矩阵第一行的第一个单元格。
B 是矩阵第一行中的第二个单元格。
Z 是矩阵第二行的第一个单元格。
Y 是矩阵第二行中的第二个单元格。
换句话说:我希望当我单击“立即计算”时给出四个随机数 - 在矩阵中 - 每次随机产生上述结果之一。
最佳答案
适应类似的东西:
Sub Yahya()
Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction
Dim detDesired As Long
result = Array(1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25)
Dim A1 As Range, A2 As Range, B1 As Range, B2 As Range, C3 As Range
Set A1 = Range("A1")
Set A2 = Range("A2")
Set B1 = Range("B1")
Set B2 = Range("B2")
Set C3 = Range("C3")
detDesired = result(wf.RandBetween(0, 12))
C3 = detDesired
A1 = wf.RandBetween(1, 20)
B1 = wf.RandBetween(-10, 10)
A2 = wf.RandBetween(-10, 10)
B2 = (C3 + A2 * B1) / A1
End Sub
我们将行列式存储在 C3 .调整随机选择的域以满足您的需要:
方法是随机选择四个矩阵元素中的行列式和三个;然后计算第四个元素(因为问题受到约束)
关于excel - 以特定顺序(符合方程式)生成随机数以产生一系列特定结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33065389/